A minimalist tutorial for getting started with Qt (3)

"Mouse Events in Qt"

Event abbreviated as QEvent
does some construction, destruction, and capture for some controls. Then you need to make some custom controls based on the existing controls.


Mouse enter event: enterEvent(QEvent *event);
mouse leave event: leaveEvent(QEvent *event);
virtual keyword, which means that the member function can be overridden in the subclass.

Use Cases:

PS: Drag and drop the type of Label control on the UI interface, right-click " Promote to... " myLabel custom type

/*
    自定义空间类
    实现鼠标进入/离开事件的捕捉。
*/
class myLabel : public QLabel
{
    Q_OBJECT
public:
    explicit myLabel(QWidget *parent = nullptr);

    /* 重写进入和离开事件 */
    void enterEvent(QEvent *event);
    void leaveEvent(QEvent *event);
signals:

public slots:
};

myLabel::myLabel(QWidget *parent) : QLabel(parent)
{

}

void myLabel::enterEvent(QEvent *event) {
    qDebug()<<"鼠标进入...";
}
void myLabel::leaveEvent(QEvent *event) {
    qDebug()<<"鼠标离开...";
}

Reimplemented Protected Functions

There are also member functions available for reimplementation in the QLabel class, as shown in the following table

Mouse Down: mousePressEvent (QMouseEvent * EV)
mouse movement: . MouseMoveEvent (QMouseEvent * EV)
Mouse Up: mouseReleaseEvent (EV QMouseEvent *)

Code

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

    /* 鼠标的进入/离开事件 */
    void enterEvent(QEvent *event);
    void leaveEvent(QEvent *event);

    /* 鼠标的移动、按下、释放 事件 */
    virtual void mouseMoveEvent(QMouseEvent *ev);
    virtual void mousePressEvent(QMouseEvent *ev);
    virtual void mouseReleaseEvent(QMouseEvent *ev);

signals:

public slots:
};

myLabel::myLabel(QWidget *parent) : QLabel(parent)
{
    // 设置鼠标追踪 默认为false
    // 鼠标不需要按下,即可打印坐标
    setMouseTracking(true);
}

void myLabel::enterEvent(QEvent *event) {
    qDebug()<<"鼠标进入...";
}
void myLabel::leaveEvent(QEvent *event) {
    qDebug()<<"鼠标离开...";
}

void myLabel::mouseMoveEvent(QMouseEvent *ev) {
    /* 参数:QMouseEvent *ev
       能捕获到所有鼠标按下、点击、释放等信息。详情可查表
    */
    // Qt中的格式化字符串
    if (ev->buttons() & Qt::LeftButton) {
        QString str = QString("鼠标移动 x=%1, y=%2 ").arg(ev->x()).arg(ev->y());
        qDebug()<<str;
    }

}
void myLabel::mousePressEvent(QMouseEvent *ev) {
    // 鼠标左键按下(状态判断用&)
    if (ev->button() == Qt::LeftButton) {
        QString str = QString("鼠标按下 x=%1, y=%2 ").arg(ev->x()).arg(ev->y());
        qDebug()<<str;
    }
}
void myLabel::mouseReleaseEvent(QMouseEvent *ev) {
    QString str = QString("鼠标释放 x=%1, y=%2 ").arg(ev->x()).arg(ev->y());
    qDebug()<<str;
}

"Timer 1-Event"

Timer event: timerEvent

Steps to start the timer event:

  1. Rewrite timer event timerEvent(QTimerEvent *e)
  2. Start the timer startTimer(1000)

Sample upgrade:

Requirements: +1 every 2 seconds.

Note that in line 19 of the upper chest, the QTimerEvent class has a member function timeId in the class, which represents the timer ID passed in by the external timer

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    // 重写定时器事件
    void timerEvent(QTimerEvent *e);
    // 定时器ID
    int id1;
    int id2;

private:
    Ui::Widget *ui;
};

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 启动定时器
    id1 = startTimer(1000); //参数1 间隔  单位  毫秒

    id2 = startTimer(2000);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e) {
    if (e->timerId() == id1) {
        static int num = 1;
        // label 每个1秒+1
        ui->label->setText(QString::number(num++));
    }
    if (e->timerId() == id2) {
        static int num = 1;
        // label_2 每隔2秒+1
        ui->label_2->setText(QString::number(num++));
    }
}

"Timer 2-Class"

  1. Use the timer class QTimer
  2. Create timer object QTimer *timer = new QTimer(this)
  3. Start the timer timer->start();
  4. Every certain milliseconds, send a signal (timeout) to monitor
  5. Pause the timer timer->stop();

Compare timer-event. The timer-type object has a single function, that is, a timer is responsible for the start and stop of a function.
For timer-events, all timer response events are placed in the timer trigger function timerEvent (QTimerEvent *e).

Test the chestnut:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 定时器第二种方式
    QTimer *timer = new QTimer(this);
    // 启动定时器
    timer->start(500);// 每隔0.5秒,定时器对象发出一个信号出来.
    connect(timer, &QTimer::timeout, [=](){
        static int num = 1;
        // label 每隔0.5秒+1
        ui->label->setText(QString::number(num++));
    });

    // 点击暂停按钮,实现停止定时器
    connect(ui->btnStop, &QPushButton::clicked, [=](){
        timer->stop();
    });
}

 

Guess you like

Origin blog.csdn.net/weixin_44937328/article/details/114389388