(十)事件,定时器
我们在QLabel控件上实现事件的demo
建好工程之后,新建一个类
修改继承类QWidget 为QLabel
拉一个label控件,提升为自定义的MyLabel
QLabel 的一些虚函数,我们可以重载实现它的一些事件
这些函数,我们只要实现他们就好了,不需要显示地去调用,类似于回调函数
Reimplemented Protected Functions virtual void changeEvent(QEvent *ev) override virtual void contextMenuEvent(QContextMenuEvent *ev) override virtual bool event(QEvent *e) override virtual void focusInEvent(QFocusEvent *ev) override virtual bool focusNextPrevChild(bool next) override virtual void focusOutEvent(QFocusEvent *ev) override virtual void keyPressEvent(QKeyEvent *ev) override virtual void mouseMoveEvent(QMouseEvent *ev) override virtual void mousePressEvent(QMouseEvent *ev) override virtual void mouseReleaseEvent(QMouseEvent *ev) override virtual void paintEvent(QPaintEvent *) override 4 protected functions inherited from QFrame 35 protected functions inherited from QWidget 9 protected functions inherited from QObject 1 protected function inherited from QPaintDevice
mylabel.cpp
#include "mylabel.h" #include <QMouseEvent> #include <QTimerEvent> #include <QTimer> // QWidget 默认是不追踪鼠标事件的 MyLabel::MyLabel(QWidget *parent) : QLabel(parent) { // 设置窗口追踪鼠标键 this->setMouseTracking(true); // 启动定时器 // 参数 1: 触发定时器的时间, 单位: ms // 参数2: 使用默认值 // 返回值: 定时器ID id = startTimer(2000); id1 = startTimer(3000); // 第二种定时器用法 // QTimer * timer = new QTimer(this); // timer->start(100); // connect(timer, &QTimer::timeout, this, [=]() // { // static int number = 0; // this->setText(QString::number(number++)); // }); } // 进入还是离开边界的一瞬间来完成的 // 鼠标进入 void MyLabel::enterEvent(QEvent *) { setText("你不要在我身上乱摸!!!!"); } // 鼠标离开 void MyLabel::leaveEvent(QEvent *) { setText("终于离开了..."); } void MyLabel::mousePressEvent(QMouseEvent *ev) { // 字符串拼接 QString().arg() // %1, %2, %3 -- 占位符 QString btn; if(ev->button() == Qt::LeftButton) { btn = "LeftButton"; } else if(ev->button() == Qt::RightButton) { btn = "RightButton"; } else if(ev->button() == Qt::MidButton) { btn = "MidButton"; } QString str = QString("MousePree[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str); } void MyLabel::mouseReleaseEvent(QMouseEvent *ev) { QString btn; if(ev->button() == Qt::LeftButton) { btn = "LeftButton"; } else if(ev->button() == Qt::RightButton) { btn = "RightButton"; } else if(ev->button() == Qt::MidButton) { btn = "MidButton"; } QString str = QString("MouseRelease[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str); } void MyLabel::mouseMoveEvent(QMouseEvent *ev) { QString btn; if(ev->buttons() & (Qt::LeftButton | Qt::RightButton)) { btn = "LeftButton"; } else if(ev->buttons() & Qt::RightButton) { btn = "RightButton"; } else if(ev->buttons() & Qt::MidButton) { btn = "MidButton"; } QString str = QString("MouseMove[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str); } // 每触发一次定时器, 进入该函数中 void MyLabel::timerEvent(QTimerEvent *e) { QString str; if(e->timerId() == id) { static int num = -100; str = QString("%1: %2").arg("Time out: ").arg(num++); if(num >= 100) { // 关闭定时器 killTimer(id); } } else if(e->timerId() == id1) { static int num1 = 10000; str = QString("%1: %2").arg("Time out: ").arg(num1++); if(num1 >= 10000+1000) { // 关闭定时器 killTimer(id1); } } setText(str); }