Qt学习(三):事件、定时器、事件过滤器
生活随笔
收集整理的這篇文章主要介紹了
Qt学习(三):事件、定时器、事件过滤器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
知識點
結(jié)果演示
mybutton.cpp
#include "mybutton.h" #include <QDebug> #include <QMouseEvent>MyButton::MyButton(QWidget *parent) :QPushButton(parent) {}void MyButton::mousePressEvent(QMouseEvent *e){//判斷是否左鍵if(e->button()==Qt::LeftButton){qDebug()<<"自定義事件按鈕被按下";//e->accept();}else{//不做處理QPushButton::mousePressEvent(e);//事件的忽略,事件繼續(xù)往下傳遞}}mybutton.h
#ifndef MYBUTTON_H #define MYBUTTON_H#include <QPushButton>class MyButton : public QPushButton {Q_OBJECT public:explicit MyButton(QWidget *parent = 0);private://重寫按鈕按下事件void mousePressEvent(QMouseEvent *e);signals:public slots:};#endif // MYBUTTON_Hmylabel.cpp
#include "mylabel.h" #include <QMouseEvent> #include <QDebug>myLabel::myLabel(QWidget *parent) :QLabel(parent) {//設(shè)置追蹤鼠標this->setMouseTracking(true); }void myLabel::mousePressEvent(QMouseEvent *ev) {//判斷是左鍵 中鍵還是右鍵按下if(ev->button()==Qt::LeftButton){qDebug()<<"這是左鍵";}else if(ev->button()==Qt::RightButton){qDebug()<<"這是右鍵";}else if(ev->button()==Qt::MidButton){qDebug()<<"這是中鍵";}int i=ev->x();int j=ev->y();QString myStr=QString("<center>點擊的坐標為:(%1 %2)</center>").arg(i).arg(j);//界面端顯示坐標this->setText(myStr); }void myLabel::mouseReleaseEvent(QMouseEvent *ev) {QString myStr=QString("<center>釋放的坐標為:(%1 %2)</center>").arg(ev->x()).arg(ev->y());//界面端顯示坐標this->setText(myStr); }void myLabel::mouseMoveEvent(QMouseEvent *ev) {QString myStr=QString("<center>移動的坐標為:(%1 %2)</center>").arg(ev->x()).arg(ev->y());//界面端顯示坐標this->setText(myStr); }void myLabel::enterEvent(QEvent *){QString text = QString("<center><h1>Mouse enter</h1></center>");this->setText(text); }//離開窗口區(qū)域 void myLabel::leaveEvent(QEvent *){QString text = QString("<center><h1>Mouse leave</h1></center>");this->setText(text); }mylabel.h
#ifndef MYLABEL_H #define MYLABEL_H#include <QWidget> #include <QLabel>class myLabel : public QLabel {Q_OBJECT public:explicit myLabel(QWidget *parent = 0);protected://鼠標點擊事件void mousePressEvent(QMouseEvent *ev);//鼠標釋放事件void mouseReleaseEvent(QMouseEvent *ev);//鼠標移動事件void mouseMoveEvent(QMouseEvent *ev);//進入窗口區(qū)域void enterEvent(QEvent *);//離開窗口區(qū)域void leaveEvent(QEvent *);signals:public slots:};#endif // MYLABEL_Hmywidget.cpp
#include "mywidget.h" #include "ui_mywidget.h" #include <QDebug> #include <QKeyEvent> #include <QMessageBox> #include <QCloseEvent>MyWidget::MyWidget(QWidget *parent) :QWidget(parent),ui(new Ui::MyWidget) {ui->setupUi(this);//定時器ID賦值timeID=this->startTimer(1000);timeID02=this->startTimer(500);//button按鈕被觸發(fā)connect(ui->pushButton,&MyButton::clicked,[=](){qDebug()<<"button按鈕被按下";});//對控件安裝事件過濾器ui->label_4->installEventFilter(this);//設(shè)置追蹤鼠標this->setMouseTracking(true); }MyWidget::~MyWidget() {delete ui; }void MyWidget::keyPressEvent(QKeyEvent *ev){//qDebug()<<ev->key();qDebug()<<char(ev->key()); }//定時器 void MyWidget::timerEvent(QTimerEvent *ev){//判斷是哪一個定時器if(ev->timerId()==this->timeID){static int myClock=0;QString myStr=QString("<center><h1>timer out: %1</h1></center>").arg(myClock++);//界面端顯示變換ui->label_3->setText(myStr);if(myClock==6){//停止定時器this->killTimer(this->timeID);qDebug()<<"1號定時器取消了!!";}}else if(ev->timerId()==this->timeID02){static int myClock=0;QString myStr=QString("<center><h1>timer out: %1</h1></center>").arg(myClock++);//界面端顯示變換ui->label_2->setText(myStr);if(myClock==100){//停止定時器this->killTimer(this->timeID02);qDebug()<<"2號定時器取消了!!";}}}//關(guān)閉提醒事件 //關(guān)閉的時候提示要不要關(guān)閉 void MyWidget::closeEvent(QCloseEvent *e){QMessageBox msgBox;msgBox.setInformativeText("Are you sure you want to close?");msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);msgBox.setDefaultButton(QMessageBox::Yes);int ans=msgBox.exec();switch (ans) {case QMessageBox::Yes:qDebug()<<"已經(jīng)關(guān)閉!!";//關(guān)閉窗口//處理關(guān)閉窗口事件,接收事件,事件就不會再往下傳遞e->accept();break;case QMessageBox::No:qDebug()<<"放棄關(guān)閉!!";//不關(guān)閉窗口//忽略事件,事件繼續(xù)給父組件傳遞e->ignore();break;default:break;} }//通過QT事件過濾器,過濾鼠標按鍵事件。使鼠標單擊無效 bool MyWidget::eventFilter(QObject *obj, QEvent *ev){if(obj==ui->label_4){QMouseEvent *env=static_cast<QMouseEvent*>(ev);//判斷事件if(ev->type() == QEvent::MouseButtonPress){ui->label_4->setText(QString("Mouse move:(%1, %2)").arg(env->x()).arg(env->y()));return true;}}return QWidget::eventFilter(obj,ev); }mywidget.h
#ifndef MYWIDGET_H #define MYWIDGET_H#include <QWidget>namespace Ui { class MyWidget; }class MyWidget : public QWidget {Q_OBJECTpublic:explicit MyWidget(QWidget *parent = 0);~MyWidget();private:Ui::MyWidget *ui;//定時器IDint timeID;//第二個定時器IDint timeID02;protected://鍵盤按下事件void keyPressEvent(QKeyEvent *);//計時器事件void timerEvent(QTimerEvent *);//關(guān)閉提醒事件void closeEvent(QCloseEvent *);//事件過濾器bool eventFilter(QObject *obj, QEvent *ev); };#endif // MYWIDGET_H 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Qt学习(三):事件、定时器、事件过滤器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt学习(二):菜单栏、工具栏和对话框
- 下一篇: Qt学习(四):qt读写文件