我们在qt编程的时候,会遇到传递一个事件到其他界面,但是通过信号槽的方式当然也可以解决,但是很多个界面都需要这个事件的时候,就会自己写太多的代码和信号槽来连接,其实我们可以通过QEvent自定义事件来达到相同的效果,QCoreApplication::sendEvent()或者QCoreApplication:;postEvent()发送自定义事件。

Qt 自定义事件实例_#define


先看测试截图,

我们通过发送自定义事件来让计数增加。同时在打印中截取事件结果。

下面是代码:

QCustomEvent.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-07-12T11:10:14
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QCustomEvent
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
mainwidget.cpp \
qxeventwidget.cpp

HEADERS += mainwidget.h \
qxeventwidget.h

FORMS += mainwidget.ui \
qxeventwidget.ui

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include "qxeventwidget.h"
namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget
{
Q_OBJECT

public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
protected:
bool eventFilter (QObject *watched, QEvent *event);
private:
Ui::MainWidget *ui;
QXEventWidget *eventw;
int num = 0;
};

#endif // MAINWIDGET_H

qxeventwidget.h

#ifndef QXEVENTWIDGET_H
#define QXEVENTWIDGET_H

#include <QWidget>
#include <QEvent>
namespace Ui {
class QXEventWidget;
}

static const QEvent::Type MyEventType = (QEvent::Type)1501;

class QXEventWidget : public QWidget
{
Q_OBJECT

public:
explicit QXEventWidget(QWidget *parent = 0);
~QXEventWidget();

private slots:
void on_pushButton_clicked();

private:
Ui::QXEventWidget *ui;
};

class MyEvent: public QEvent
{
public:
MyEvent(Type MyEventType):QEvent(MyEventType){}
};

#endif // QXEVENTWIDGET_H

main.cpp

#include "mainwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();
a.installEventFilter(&w);//安装事件过滤器
return a.exec();
}

mainwidget.cpp

#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDebug>
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
eventw = new QXEventWidget(this);
eventw->setGeometry (0,0,400,300);
num = 0;
}

MainWidget::~MainWidget()
{
delete ui;
}

bool MainWidget::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == 1501)
{
qDebug()<<"MyEventType="<<MyEventType;
qDebug()<<"accept MyEventType";
ui->num_label->setText (QString::number (num++));
return false;
}
return QObject::eventFilter(watched,event);
}

qxeventwidget.cpp

#include "qxeventwidget.h"
#include "ui_qxeventwidget.h"
#include <QDebug>
QXEventWidget::QXEventWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QXEventWidget)
{
ui->setupUi(this);
QEvent::registerEventType(QEvent::User+500);
}

QXEventWidget::~QXEventWidget()
{
delete ui;
}

void QXEventWidget::on_pushButton_clicked()
{
MyEvent myEvent(MyEventType);
MyEvent myEvent1(MyEventType);
QApplication::sendEvent(this,&myEvent1);
qDebug()<<"sendEvent";
}



ui文件上传没用,你们自己画一个吧。