咱們在QLabel控件上實現事件的demoide
QLabel 的一些虛函數,咱們能夠重載實現它的一些事件函數
這些函數,咱們只要實現他們就行了,不須要顯示地去調用,相似於回調函數this
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.cppspa
#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); }