Qt中可拖动的圆形QLabel

7 篇文章 7 订阅

自定义label头文件

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QWidget>
#include <QLabel>
#include <QMoveEvent>
#include <QEvent>
#include <QMouseEvent>
#include <QPoint>
#include <QDebug>
#include <QTimer>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = nullptr);

signals:
protected:
    void mousePressEvent(QMouseEvent *ev);
    void mouseMoveEvent(QMouseEvent *ev);
    void mouseReleaseEvent(QMouseEvent *ev);

public slots:
    void msgCountAdd();
    void msgAdd();
    void msgSub();
private:
    QPoint dragPosition;
    QWidget *m_pParent;
    int m_nMovePointX;
    int m_nMovePointY;
    QTimer *m_pTimer;
    int m_nMsgCount;
};

#endif // MYLABEL_H

自定义label源文件

#include "Mylabel.h"

MyLabel::MyLabel(QWidget *parent) : QLabel(parent), m_nMsgCount(0), m_pParent(parent)
{
    const QString labelStyleStr = "min-width: 32px; min-height: 32px;border-radius: 16px;  border:2px solid yellow;background:red;color:white;font:14pt 微软雅黑";
    setStyleSheet(labelStyleStr);
    setAlignment(Qt::AlignCenter);
    m_pTimer = new QTimer;
    connect(m_pTimer,&QTimer::timeout,this,&MyLabel::msgCountAdd);
    //m_pTimer->start(300);

}

void MyLabel::mousePressEvent(QMouseEvent *ev)
{
    if(ev->button() == Qt::LeftButton)
    {
        dragPosition = ev->globalPos()-frameGeometry().topLeft();
        ev->accept();

    }
}
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
    if(ev->buttons() & Qt::LeftButton)
    {
        qDebug() << ev->globalPos()- dragPosition;
        m_nMovePointX = (ev->globalPos()- dragPosition).x();
        m_nMovePointY = (ev->globalPos()- dragPosition).y();
        if(m_nMovePointX < 0 || m_nMovePointY < 0 || m_nMovePointY > m_pParent->rect().bottom() - this->rect().width() || m_nMovePointX > m_pParent->rect().right() - this->rect().width())
        {
            //防止移出顶部和底部
            if(m_nMovePointY > m_pParent->rect().bottom() - this->rect().width() || m_nMovePointY < 0)
            {
                move((ev->globalPos() - dragPosition).x(), this->geometry().y());
            }
            //防止移出左侧和右侧
            if(m_nMovePointX > m_pParent->rect().right() - this->rect().width() || m_nMovePointX < 0)
            {
                move(this->geometry().x(),(ev->globalPos() - dragPosition).y());
            }
            //防止移出左上角
            if(m_nMovePointX < 0 && m_nMovePointY < 0)
            {
                move(0,0);
            }
            //防止移出右小角
            if(m_nMovePointY > m_pParent->rect().bottom() - this->rect().width() && m_nMovePointX > m_pParent->rect().right() - this->rect().width())
            {
                move(m_pParent->rect().right() - this->rect().width(), m_pParent->rect().bottom() - this->rect().height());
            }
            //防止移出右上角
            if(m_nMovePointY < 0 && m_nMovePointX > m_pParent->rect().right() - this->rect().width())
            {
                move(m_pParent->rect().right() - this->rect().width(),0);
            }
            //防止移出左上角
            if(m_nMovePointX < 0 && m_nMovePointY > m_pParent->rect().bottom() - this->rect().height())
            {
                move(0,m_pParent->rect().bottom() - this->rect().height());
            }

        }
        else
        {
            move(ev->globalPos() - dragPosition);
        }
    }
}

效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆的菜菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值