概述
EventLabel.h
#pragma once
#include <QLabel>
class EventLabel :
public QLabel
{
public:
EventLabel();
~EventLabel();
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
bool event(QEvent *e);
};
EventLabel.cpp
#include "EventLabel.h"
#include <QMouseEvent>
#include "ChangeNumberEvent.h"
EventLabel::EventLabel()
{
}
EventLabel::~EventLabel()
{
}
void EventLabel::mouseMoveEvent(QMouseEvent * event1)
{
QString msg;
msg.sprintf("<center><h1>Move: (%d, %d)</h1></center>",
event1->x(), event1->y());
this->setText(msg);
}
void EventLabel::mousePressEvent(QMouseEvent * event)
{
QString msg;
msg.sprintf("<center><h1>Press: (%d, %d)</h1></center>",
event->x(), event->y());
this->setText(msg);
}
void EventLabel::mouseReleaseEvent(QMouseEvent * event)
{
QString msg;
msg.sprintf("<center><h1>Release: (%d, %d)</h1></center>",
event->x(), event->y());
this->setText(msg);
}
bool EventLabel::event(QEvent * e)
{
if (e->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
if (keyEvent->key() == Qt::Key_Tab) {
QString msg;
msg.sprintf("<center><h1>event: (%d, %d)</h1></center>");
this->setText(msg);
return true;
}
}
return QWidget::event(e);
}
ChangeNumberEvent.h
#pragma once
#include <qcoreevent.h>
#include <QString>
class ChangeNumberEvent :
public QEvent
{
public:
ChangeNumberEvent(QObject* parent, QString number);
~ChangeNumberEvent();
public:
QString m_Number;
};
ChangeNumberEvent.cpp
#include "ChangeNumberEvent.h"
//自定义事件ID
//自定义事件ID范围是从QEvent::User(1000)到QEvent::MaxUser(65535)
//可以通过在前一次自定义事件ID的基础上加一的方式定义所有的自定义事件ID
const int CustomEventBaseID = QEvent::User;
const int ChangeNumberEventID = CustomEventBaseID + 1;
ChangeNumberEvent::ChangeNumberEvent(QObject* parent, QString number)
:QEvent(QEvent::Type(ChangeNumberEventID)),
m_Number(number)
{
}
ChangeNumberEvent::~ChangeNumberEvent()
{
}
QT_Test.h
#pragma once
#include <QtWidgets/QDialog>
#include "ui_QT_Test.h"
class QT_Test : public QDialog
{
Q_OBJECT
public:
QT_Test(QApplication* pApp, QWidget *parent = Q_NULLPTR);
//(QApplication* pApp, QWidget *parent = Q_NULLPTR);
protected:
bool eventFilter(QObject *watched, QEvent *event);
void customEvent(QEvent *event);
public slots:
void onClickButton1();
private:
Ui::QT_TestClass ui;
QApplication* m_pApp = nullptr;
};
QT_Test.cpp
#include "QT_Test.h"
#include <QFileDialog.h>
#include <qstring.h>
#include "EventLabel.h"
#include <QMouseEvent>
#include "ChangeNumberEvent.h"
QT_Test::QT_Test(QApplication* pApp, QWidget *parent)
: QDialog(parent)
, m_pApp(pApp)
{
ui.setupUi(this);
connect(ui.btn1, SIGNAL(clicked()), this, SLOT(onClickButton1()));
}
void QT_Test::onClickButton1()
{
//发送事件
QString curNumber = ui.edit1->text();// spinBox->value();
ChangeNumberEvent e(this, curNumber);
m_pApp->sendEvent(this, &e);
EventLabel *label = new EventLabel;
label->setWindowTitle("MouseEvent Demo");
label->installEventFilter(this);
label->resize(300, 200);
label->show();
}
bool QT_Test::eventFilter(QObject * watched, QEvent * event)
{
//object == target &&
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Tab) {
QString msg;
msg.sprintf("<center><h1>event: (%d, %d)</h1></center>");
// this->setText(msg);
return true;
}
else {
return false;
}
}
return QDialog::eventFilter(watched, event);
}
void QT_Test::customEvent(QEvent * event)
{
//ChangeNumberEvent* e = dynamic_cast<ChangeNumberEvent*>(event);
//if (e)
//{
// ui.la1->setText(e->m_Number);
// ui.la2->setText(e->m_Number);
//}
}
最后
以上就是生动往事为你收集整理的QT事件、事件过滤、自定义事件练习的全部内容,希望文章能够帮你解决QT事件、事件过滤、自定义事件练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复