Qt笔记——Event
#ifndef MYBUTTON_H
#define MYBUTTON_H #include <QPushButton> class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = nullptr); protected:
void mousePressEvent(QMouseEvent *e);
signals: public slots:
}; #endif // MYBUTTON_H
#include "mybutton.h"
#include <QMouseEvent>
#include <QDebug>
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{ } void MyButton::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
qDebug()<<"left mousebutton"; e->ignore();
}
else
{
QPushButton::mousePressEvent(e);
}
}
#ifndef MYLABEL_H
#define MYLABEL_H #include <QLabel> class mylabel : public QLabel
{
Q_OBJECT
public:
explicit mylabel(QWidget *parent = nullptr);
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
#include "mylabel.h"
#include <QMouseEvent>
#include <QEvent>
mylabel::mylabel(QWidget *parent) : QLabel(parent)
{
this->setMouseTracking(true);
} void mylabel::mousePressEvent(QMouseEvent *ev)
{
int i = ev -> x();
int j = ev -> y();
// QString str = QString("abc %1 ^_^ %2").arg(123).arg("hello");
if (ev->button() == Qt::LeftButton)
{ }else if(ev->button() == Qt::RightButton)
{ }else if(ev->button() == Qt::MiddleButton)
{ }
QString text = QString("<center><h1>Mouse Press: (%1, %2)</h1></center>")
.arg(i).arg(j);
this->setText(text);
} void mylabel::mouseReleaseEvent(QMouseEvent *ev)
{
QString text = QString("<center><h1>Mouse Release: (%1, %2)</h1></center>")
.arg(ev -> x()).arg(ev -> y());
this->setText(text);
} void mylabel::mouseMoveEvent(QMouseEvent *ev)
{
QString text = QString("<center><h1>Mouse Move: (%1, %2)</h1></center>")
.arg(ev -> x()).arg(ev -> y());
//this->setText(text);
} void mylabel::enterEvent(QEvent *e)
{
QString text = QString("<center><h1>Mouse enter</h1></center>");
this->setText(text);
} void mylabel::leaveEvent(QEvent *e)
{
QString text = QString("<center><h1>Mouse leave</h1></center>");
this->setText(text);
}
#ifndef MYWIDGET_H
#define MYWIDGET_H #include <QWidget> namespace Ui {
class MyWidget;
} class MyWidget : public QWidget
{
Q_OBJECT public:
explicit MyWidget(QWidget *parent = );
~MyWidget(); protected:
void keyPressEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
void closeEvent(QCloseEvent *event); bool eventFilter(QObject *watched, QEvent *event);
private:
Ui::MyWidget *ui;
int timerId;
int timerId2;
}; #endif // MYWIDGET_H
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QKeyEvent>
#include <QDebug>
#include <QMessageBox>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
timerId = this->startTimer();
timerId2 = this->startTimer(); connect(ui->pushButton,&MyButton::clicked,
[=]()
{
qDebug()<<"press now";
}); ui->label_2->installEventFilter(this);
} void MyWidget::keyPressEvent(QKeyEvent *event)
{
qDebug()<<(char)(event->key());
if (event->key() == Qt::Key_A)
{
qDebug()<<"Qt::Key_A";
} } void MyWidget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == this->timerId)
{
static int sec = ;
ui->label->setText(QString("<center><h1>Timer: (%1)</h1></center>")
.arg(sec++));
if(==sec)
{
this->killTimer(timerId);
}
}
else if(event->timerId() == this->timerId2)
{
static int sec = ;
ui->label_2->setText(QString("<center><h1>Timer: (%1)</h1></center>")
.arg(sec++)); }
} void MyWidget::closeEvent(QCloseEvent *event)
{
int ret = QMessageBox::question(this,"question","close it?");
if (ret == QMessageBox::Yes)
{
event->accept();
}
else
{
event->ignore();
}
} bool MyWidget::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->label_2)
{
QMouseEvent *env = static_cast<QMouseEvent*>(event);
if(event->type() == QEvent::MouseMove)
{
ui->label_2->setText(QString("Mouse move:(%1, %2)").arg(env->x()).arg(env->y()));
return true;
}
}
else
{
return QWidget::eventFilter(watched,event);
}
} MyWidget::~MyWidget()
{
delete ui;
}
#include "mywidget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show(); return a.exec();
}

Qt笔记——Event的更多相关文章
- QT笔记之实现阴影窗口
方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...
- QT笔记之自定义窗口拖拽移动
1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...
- Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)
QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...
- Qt笔记之使用设计器自定义窗口标题栏
1.在窗口显示之前,设置WindowFlags为FramelessWindowHint,以产生一个没有边界的窗口 例如 Widget::Widget(QWidget *parent) : QWidge ...
- QT笔记之解决QT5.2.0和VS2012中文乱码 以及在Qt Creator中文报错
转载:http://bbs.csdn.net/topics/390750169 VS2012 中文乱码 1.方法一: 包含头文件 #include <QTextCodec> ....... ...
- Qt事件过滤器Event Filter
事件过滤器针对一类或者多种不同类型的对象,定义了重写操作. 简单的说: 1.先对UI对象注册Event Filter 例如: ui->drawBtn->installEventFil ...
- QT笔记之VS开发程序遇到的问题
转载:http://www.cnblogs.com/li-peng/p/3644812.html 转载:http://www.cnblogs.com/csuftzzk/p/VS_Qt_Experien ...
- QT笔记之不规则窗口的实现
QT实现的不规则窗口,是根据图片的形状显示 1.去标题栏 2.设置窗口背景为透明色 3.最后给窗口设置背景色 注:背景图为镂空的 格式为.png 图片资源下载:http://pan.baidu.com ...
- QT笔记之模态对话框及非模态对话框
模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...
随机推荐
- 【工作感悟】——xyb项目部署
[前言] 接手xyb项目维护有一段时间了,除了熟悉业务需求和开发环境外,还没有进行新需求的开发.前几天突然接到一个任务,要去发改委给他们部署一版最新的系统.本来事情也不大,也没有很难.但是本来是大屈. ...
- wait_event_interruptible_timeout
最近一套方案涉及到内核线程之间的同步,用到了函数wait_event_interruptible_timeout函数,大致是这样: A:是一个后台的线程,平常没事就睡觉,有时被唤醒,或者每5分钟醒一次 ...
- BZOJ4399 魔法少女LJJ(线段树合并)
注意到只有增加点/合并的操作.这些操作都可以用线段树完成,于是线段树合并一发就好了.注意乘积大小直接比较肯定会炸,取个对数即可.数据中存在重边. #include<iostream> #i ...
- git config文件总结及git alias配置
1. 文件位置 mac /etc/.gitconfig 系统级~/.gitconifg 用户级(覆盖系统级) windows C:\Users\$user\.gitconfig 当前项目下 .git/ ...
- bzoj3196 [TYVJ1730]二逼平衡树 树套树 线段树套替罪羊树
人傻自带大常数 二分的可行性证明: 贴近他的正确答案不会被当作次优解删掉,因为,若二分在他右边发生,那么二分一定会把左边作为优解,左边同理,所以他一定是被扣掉的所以最后一个小于等于一定是正确答案 #i ...
- Fabric证书解析
一.证书目录解析 通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下: ca: 存放组织的根证书和对应的私 ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path
D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...
- Saruman’s Level Up~(多校赛算组合数)
Description Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out o ...
- Web项目中加载Spring配置的常用方法
1.web.xml中添加配置 <web-app> <context-param> <param-name>contextConfigLoc ...
- 【数据结构】bzoj1651专用牛棚
Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will onl ...