Qt mouse common events

It’s the same as the previous case, it’s also improved and the same parent class is changed, but the method is different.

First add the label label to widget.ui. At this time, its parent class is QLabel, and then you want to capture the mouse on QLabel. So we need to upgrade QLabel to its own framework, and then after customizing the framework, we can capture the information ourselves. Then add new files mylabel.h and mylabel.cpp, and choose to inherit the base class as QWidget. Because the option does not have QlABEL, you can modify the function in the file later.

#ifndef MYLABLE_H
#define MYLABLE_H

#include <QLabel>

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

    //鼠标进入  声明
    void enterEvent(QEvent *);

    //鼠标离开
    void leaveEvent(QEvent *);

    //鼠标按下事件
    void mousePressEvent(QMouseEvent *ev);
    //鼠标离开事件
    void mouseReleaseEvent(QMouseEvent *ev) ;
    //鼠标移动事件
    void mouseMoveEvent(QMouseEvent *ev) ;
signals:

};

#endif // MYLABLE_H
#include "mylable.h"
#include "QDebug"
#include "QMouseEvent"
MyLable::MyLable(QWidget *parent) : QLabel(parent)
{
    //设置鼠标追踪
    this->setMouseTracking(true);//默认为false这里设置为true
}


//两个父类一样才能提升

//此时提升了widget.ui父类为mylable    此时就可以把widget.ui当为mylable进行代码添加使用
//鼠标进入
void MyLable::enterEvent(QEvent *)//加上作用域
{
//    qDebug()<<"鼠标进入了";
}

//鼠标离开
void MyLable::leaveEvent(QEvent *)
{
//    qDebug()<<"鼠标离开了";
}


//鼠标按下事件
void MyLable::mousePressEvent(QMouseEvent *ev)
{
    //如果是鼠标左键按下,才打印下面的信息
    if(ev->button()==Qt::LeftButton)
    {
        QString str=QString("鼠标按下了,x=%1,y=%2").arg(ev->x()).arg(ev->y());//QString函数
        qDebug()<<str;
    }

}
//鼠标离开事件
void MyLable::mouseReleaseEvent(QMouseEvent *ev)
{
    if(ev->button()==Qt::LeftButton){
        QString str=QString("鼠标释放了,x=%1,y=%2").arg(ev->x()).arg(ev->y());
        qDebug()<<str;//
    }

}
//鼠标移动事件
void MyLable::mouseMoveEvent(QMouseEvent *ev)
{
    //移动不是一瞬间的,是一段时间的
//    if(ev->buttons() & Qt::LeftButton)//用了复合按键和未与按键
//    {
        QString str=QString("鼠标移动了,x=%1,y=%2").arg(ev->x()).arg(ev->y());//QString函数
        qDebug()<<str;//要长按
//    }

}//坐标系是基于坐标系的

It has been implemented that when the mouse enters the label box, qDebug will move, including clicking and leaving, and there will be a response.

Code in depth

//Mouse press event 
void MyLable::mousePressEvent(QMouseEvent *ev) 
{     
//If the left mouse button is pressed, the following information will be printed    if(ev->button()==Qt::LeftButton)     
{         QString str =QString("The mouse is pressed, x=%1,y=%2").arg(ev->x()).arg(ev->y()); //QString function
 
        qDebug()<<str;    }}

The first of the two red codes is to press the left button to achieve the effect. The second is to display the coordinates in the QLable. It is based on the upper left corner as (0,0) coordinates. After implementation, you can always go out and the coordinates can be is negative

//Settings in the constructor 
MyLable::MyLable(QWidget *parent) : QLabel(parent)
{ //Set mouse tracking  
   this->setMouseTracking(true) ; //The default is false and here it is set to true}

The default is false, here it is set to true

The renderings are as follows 

 

Guess you like

Origin blog.csdn.net/weixin_61847358/article/details/135439145