Qt 自定义事件实例

我们在qt编程的时候,会遇到传递一个事件到其他界面,但是通过信号槽的方式当然也可以解决,但是很多个界面都需要这个事件的时候,就会自己写太多的代码和信号槽来连接,其实我们可以通过QEvent自定义事件来达到相同的效果,QCoreApplication::sendEvent()或者QCoreApplication:;postEvent()发送自定义事件。
这里写图片描述
先看测试截图,
我们通过发送自定义事件来让计数增加。同时在打印中截取事件结果。
下面是代码:
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文件上传没用,你们自己画一个吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值