知识点
- mybutton.cpp 判断是否左键
- mylabel.cpp 判断是左键 中键还是右键按下、进入离开窗口区域的显示
- mywidget.cpp 定时器 事件过滤器 关闭提醒事件,关闭的时候提示要不要关闭
结果演示
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);
//事件的忽略,事件继续往下传递
}
}
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_H
mylabel.cpp
#include "mylabel.h"
#include <QMouseEvent>
#include <QDebug>
myLabel::myLabel(QWidget *parent) :
QLabel(parent)
{
//设置追踪鼠标
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);
}
//离开窗口区域
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);
//进入窗口区域
void enterEvent(QEvent *);
//离开窗口区域
void leaveEvent(QEvent *);
signals:
public slots:
};
#endif // MYLABEL_H
mywidget.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按钮被触发
connect(ui->pushButton,&MyButton::clicked,
[=](){
qDebug()<<"button按钮被按下";
}
);
//对控件安装事件过滤器
ui->label_4->installEventFilter(this);
//设置追踪鼠标
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号定时器取消了!!";
}
}
}
//关闭提醒事件 //关闭的时候提示要不要关闭
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()<<"已经关闭!!";
//关闭窗口
//处理关闭窗口事件,接收事件,事件就不会再往下传递
e->accept();
break;
case QMessageBox::No:
qDebug()<<"放弃关闭!!";
//不关闭窗口
//忽略事件,事件继续给父组件传递
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_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private:
Ui::MyWidget *ui;
//定时器ID
int timeID;
//第二个定时器ID
int timeID02;
protected:
//键盘按下事件
void keyPressEvent(QKeyEvent *);
//计时器事件
void timerEvent(QTimerEvent *);
//关闭提醒事件
void closeEvent(QCloseEvent *);
//事件过滤器
bool eventFilter(QObject *obj, QEvent *ev);
};
#endif // MYWIDGET_H