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所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...
随机推荐
- lintcode-76-最长上升子序列
76-最长上升子序列 给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度. 说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列 ...
- 【转】C++ const用法 尽可能使用const
http://www.cnblogs.com/xudong-bupt/p/3509567.html C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不 ...
- gdb查看内存中所有的信息
他们会把做内核的人当成无所不能的,认为你们对反编译啥的都应该会. 俗话说的好,人要活成别人想要的样子嘛: 看下如何停止进程,让大家看到内存中到底是啥样子; 简单的print globalA当然能输出来 ...
- BZOJ4476 JSOI2015送礼物(分数规划+单调队列)
看到这个式子当然先二分答案.得max-min-(j-i+k)ans>=0. 显然max-min相同的情况下所选区间长度越短越好,所以max和min都应该取在边界.那么实际上我们根本不用管端点是否 ...
- ACM 竞赛高校联盟 练习赛 第二场 B&C
B. 题解: 枚举约数即可,判断n个数能否填约数的整数倍 #include <iostream> #include <cstring> #include <cstdio& ...
- [hihocoder 1050]求树的最长链
题目链接:http://hihocoder.com/problemset/problem/1050 两种方法: 1. 两遍dfs,第一次随便找一个根,找到距离这个根最远的点,这个点必然是最长链的一端. ...
- bzoj 2525 [Poi2011]Dynamite 二分+树形dp
[Poi2011]Dynamite Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 270 Solved: 138[Submit][Status][D ...
- gitlab7.2安装
系统:centos6.4 1.安装依赖包 导入epel: useradd git wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-rel ...
- com.mongodb.MongoException$CursorNotFound: cursor not found on server异常处理
java链接MongoDB处理大量数据时经常碰到cursor not found 的异常,其实是超时所致 Exception in thread "main" com.mongod ...
- COGS2642 / Bzoj4590 [Shoi2015]自动刷题机
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 906 Solved: 321 Description 曾经发明了信号增幅仪的发明家SHTSC又公开了 ...