QT笔记——QT自定义事件

我们有时候想发送自定义事件

1:创建自定义事件,首先我们需要知道它的条件

1:自定义事件需要继承QEvent
2:事件的类型需要在 QEvent::User 和 QEvent::MaxUser 范围之间,在QEvent::User之前 是预留给系统的事件
3:使用sendEvent() 和 postEvent()发送自定义消息
4:接受自定义消息,通过 重写 customEvent 或者 使用 eventFilter事件 来处理

2:注册自定义事件ID

在注册自定义事件的时候我们有两种方法
一:使用QEvent::registerEventType(),这个是系统会帮助我们注册ID,如果错误 返回-1
二:我们自定义 取QEvent::User 和QEvent::MaxUser 之间的值

在这里插入图片描述

方法一:
QEvent::Type CustomEvent::m_eventType = (QEvent::Type)QEvent::registerEventType();

方法二:
const int CustomEventBaseID = QEvent::User;
const int TextEventID = CustomEventBaseID + 1;

3:自定义事件

class CustomEvent :public QEvent
{
public:
    CustomEvent();
    virtual ~CustomEvent();
    
	static QEvent::Type eventType();
private:
	static QEvent::Type m_eventType;
};
QEvent::Type CustomEvent::m_eventType = (QEvent::Type)QEvent::registerEventType();//1、注册自定义事件类型

CustomEvent::CustomEvent()
	:QEvent(eventType()) //2、指定事件类型
{

}

CustomEvent::~CustomEvent()
{

}

QEvent::Type CustomEvent::eventType()
{
	return m_eventType;
}

4:发送自定义事件

postEvent 方法:异步发送,向事件队列中 的发送一个指定接受者事件
不需要手动释放事件的内存,QT框架自动释放

QT笔记——QT自定义事件_第1张图片

sendEvent方法:此方法是阻塞发送的,它发送对象事件,等待对象处理结束返回;
需要手动释放事件的内存

QT笔记——QT自定义事件_第2张图片

5:自定义事件处理

写自定义事件 void customEvent(QEvent* event) override;

写事件过滤器 bool eventFilter(QObject* watched, QEvent* event) override;
还需要将对象添加到事件过滤器中

源码:
.h

#pragma once

#include 
#include "ui_CustomEventTest.h"
#include 
#include 

//自定义事件
class CustomEvent :public QEvent
{
public:
    CustomEvent();
    virtual ~CustomEvent();

    void SetText(const QString& str);
    QString GetText() const;

	void SetNumber(int number);
	int GetNumber() const;

	static QEvent::Type eventType();
private:
    QString m_str;
    int m_number;

	static QEvent::Type m_eventType;
};

class CustomTwoEvent :public QEvent
{
public:
    CustomTwoEvent();
	virtual ~CustomTwoEvent();

	void SetStr(const QString& str);
	QString GetStr() const;

    void SetNumber(int number);
    int GetNumber() const;
private:
	QString m_str;
    int m_number;

};

//测试自定义事件
class CustomEventTest : public QWidget
{
    Q_OBJECT

public:
    CustomEventTest(QWidget *parent = nullptr);
    ~CustomEventTest();
    
private slots:
    //测试自定义事件一
    void on_btn1_1_clicked();
    void on_btn2_1_clicked();
    void on_btn3_1_clicked();
    void on_btn4_1_clicked();

protected:
	void customEvent(QEvent* event) override;

private slots:
    //测试自定义事件二
    void on_btn5_1_clicked();
    void on_btn6_1_clicked();
    void on_btn7_1_clicked();
    void on_btn8_1_clicked();

protected:
    bool eventFilter(QObject* watched, QEvent* event) override;

private:
    Ui::CustomEventTestClass ui;
};

.cpp

#include "CustomEventTest.h"

/************************自定义事件一********************************/
QEvent::Type CustomEvent::m_eventType = (QEvent::Type)QEvent::registerEventType();//1、注册自定义事件类型

CustomEvent::CustomEvent()
	:QEvent(eventType()) //2、指定事件类型
{

}

CustomEvent::~CustomEvent()
{

}

void CustomEvent::SetText(const QString& str)
{
	m_str = str;
}

QString CustomEvent::GetText() const
{
	return m_str;
}

void CustomEvent::SetNumber(int number)
{
	m_number = number;
}

int CustomEvent::GetNumber() const
{
	return m_number;
}

QEvent::Type CustomEvent::eventType()
{
	return m_eventType;
}

/************************自定义事件一********************************/

/************************自定义事件二********************************/

const int CustomEventBaseID = QEvent::User;
const int TextEventID = CustomEventBaseID + 1;

CustomTwoEvent::CustomTwoEvent()
	:QEvent(QEvent::Type(TextEventID))
{

}

CustomTwoEvent::~CustomTwoEvent()
{

}

void CustomTwoEvent::SetStr(const QString& str)
{
	m_str = str;
}

QString CustomTwoEvent::GetStr() const
{
	return m_str;
}

void CustomTwoEvent::SetNumber(int number)
{
	m_number = number;
}

int CustomTwoEvent::GetNumber() const
{
	return m_number;
}

/************************自定义事件二********************************/

/************************测试自定义事件********************************/
CustomEventTest::CustomEventTest(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

	qApp->installEventFilter(this);
}

CustomEventTest::~CustomEventTest()
{

}

void CustomEventTest::on_btn1_1_clicked()
{
	QString str = QStringLiteral("自定义事件一 我是post 发送指针");
	CustomEvent* event = new CustomEvent;
	event->SetText(str);
	event->SetNumber(1);

	qApp->postEvent(this, event);
}

void CustomEventTest::on_btn2_1_clicked()
{
	QString str = QStringLiteral("自定义事件一  我是post 发送局部变量");
	CustomEvent event;
	event.SetText(str);
	event.SetNumber(2);

	//会出现崩溃
	qApp->postEvent(this, &event);
}

void CustomEventTest::on_btn3_1_clicked()
{
	QString str = QStringLiteral("自定义事件一  我是send 发送指针");
	CustomEvent* event = new CustomEvent;
	event->SetText(str);
	event->SetNumber(3);

	qApp->sendEvent(this, event);
	delete event;
}

void CustomEventTest::on_btn4_1_clicked()
{
	QString str = QStringLiteral("自定义事件一  我是send 发送局部变量");
	CustomEvent event;
	event.SetText(str);
	event.SetNumber(4);

	qApp->sendEvent(this, &event);
}

void CustomEventTest::on_btn5_1_clicked()
{
	QString str = QStringLiteral("自定义事件二  我是post 发送指针");
	CustomTwoEvent* event = new CustomTwoEvent;
	event->SetStr(str);
	event->SetNumber(5);

	qApp->postEvent(this, event);
}

void CustomEventTest::on_btn6_1_clicked()
{
	QString str = QStringLiteral("自定义事件二  我是post 发送局部变量");
	CustomTwoEvent event;
	event.SetStr(str);
	event.SetNumber(6);

	//会出现崩溃
	qApp->postEvent(this, &event);
}

void CustomEventTest::on_btn7_1_clicked()
{
	QString str = QStringLiteral("自定义事件二  我是send 发送指针");
	CustomTwoEvent* event = new CustomTwoEvent;
	event->SetStr(str);
	event->SetNumber(7);

	qApp->sendEvent(this, event);
	delete event;
}

void CustomEventTest::on_btn8_1_clicked()
{
	QString str = QStringLiteral("自定义事件二  我是send 发送局部变量");
	CustomTwoEvent event;
	event.SetStr(str);
	event.SetNumber(8);

	qApp->sendEvent(this, &event);
}

void CustomEventTest::customEvent(QEvent* event)
{
	if (event->type() == CustomEvent::eventType())
	{
		CustomEvent* e = dynamic_cast<CustomEvent*>(event);
		if (e->GetNumber() == 1)
			ui.btn1->setText(e->GetText());
		else if (e->GetNumber() == 2)
			ui.btn2->setText(e->GetText());
		else if (e->GetNumber() == 3)
			ui.btn3->setText(e->GetText());
		else if (e->GetNumber() == 4)
			ui.btn4->setText(e->GetText());
	}
}

bool CustomEventTest::eventFilter(QObject* watched, QEvent* event)
{
	if (event->type() == TextEventID)
	{
		CustomTwoEvent* e = dynamic_cast<CustomTwoEvent*>(event);
		if (e->GetNumber() == 5)
			ui.btn5->setText(e->GetStr());
		else if (e->GetNumber() == 6)
			ui.btn6->setText(e->GetStr());
		else if (e->GetNumber() == 7)
			ui.btn7->setText(e->GetStr());
		else if (e->GetNumber() == 8)
			ui.btn8->setText(e->GetStr());

		event->accept();
	}

	return QWidget::eventFilter(watched,event);
}
/************************测试自定义事件********************************/

错误:在上面的代码中,当我们post 局部变量的时候会出现错误

QCoreApplication::removePostedEvent: Event of type 65535 deleted while posted to CustomEventTest CustomEventTestClass

原因:postEvent 只能在堆上创建,系统会自动释放

参考博客:
Qt 自定义事件
Qt中的事件- 自定义事件
Qt事件:自定义事件

你可能感兴趣的:(qt,笔记,开发语言)