QT自定义事件

前言

	自定义事件主要用于用户实现自己所需要的变化

1.自定义事件的定义
自定义事件从user:1000 max:MaxUser(65535),本例子我们简单更改界面颜色

#pragma once

#include <QEvent>
#include<QColor>
const int CustomEventBaseID = QEvent::User;
const int ChangeNumberEventID = CustomEventBaseID + 1;
class ChangeColorEvent : public QEvent
{

public:
	ChangeColorEvent(QObject *parent, QColor clr);
	~ChangeColorEvent();
	QColor clr;
};


#include "ChangeColorEvent.h"

ChangeColorEvent::ChangeColorEvent(QObject *parent, QColor clr)
	: QEvent(QEvent::Type(ChangeNumberEventID))
{
	this->clr = clr;
}

ChangeColorEvent::~ChangeColorEvent()
{
}

2.自定义事件的调用
主要有sendevent和popevent两种方式,本文采用sendevent。

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication5.h"
#include"ChangeColorEvent.h"
#include<QTimer>
class QtWidgetsApplication5 : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsApplication5(QWidget *parent = Q_NULLPTR);
protected:
	void customEvent(QEvent *event);
private slots:
	void randdomData();//随机颜色
private:
    Ui::QtWidgetsApplication5Class ui;
	QColor clr;
	QApplication* m_pApp = nullptr;
	QTimer *timer;//定时刷新
};


#include "QtWidgetsApplication5.h"
#include"qthread.h"
#include<QDebug>
QtWidgetsApplication5::QtWidgetsApplication5(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(randdomData()));
	timer->start(100);
	randdomData();
}

void QtWidgetsApplication5::customEvent(QEvent * event)
{
	ChangeColorEvent* e = dynamic_cast<ChangeColorEvent*>(event);
	if (e)
	{

		QPalette pal(this->palette());
		qDebug() << e->clr;
		pal.setColor(QPalette::Background, e->clr);
		this->setAutoFillBackground(true);
		this->setPalette(pal);
		this->show();

	}
}
void QtWidgetsApplication5::randdomData()
{

		QColor clr2(rand() % 256, rand() % 256, rand() % 256);

		if (clr != clr2)
		{
			clr = clr2;
			ChangeColorEvent e(this, clr);//构造函数传参
			m_pApp->sendEvent(this, &e);
		}
}


3.效果

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,可以通过自定义控件的鼠标事件来实现对鼠标操作的响应。下面是一般的步骤: 1. 继承QWidget或QAbstractButton等基类,创建自定义控件类。 2. 重写自定义控件类的鼠标事件处理函数,如mousePressEvent、mouseReleaseEvent、mouseMoveEvent等。 3. 在重写的鼠标事件处理函数中,根据需要实现相应的功能逻辑。 例如,如果你想在自定义控件上实现鼠标点击事件的响应,可以按照以下步骤进行: 1. 创建一个继承自QWidget的自定义控件类,例如MyWidget。 2. 在MyWidget类中重写mousePressEvent函数,该函数会在鼠标按下时被调用。 3. 在mousePressEvent函数中实现你想要的功能,比如显示一个提示框或改变控件的状态等。 下面是一个简单的示例代码: ```cpp #include <QWidget> #include <QMouseEvent> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) {} protected: void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { // 左键点击事件处理逻辑 // 例如显示一个提示框 qDebug() << "Left button pressed!"; } // 调用父类的事件处理函数,保证其他事件正常处理 QWidget::mousePressEvent(event); } }; ``` 在上述示例中,我们重写了MyWidget类的mousePressEvent函数,并在函数中判断鼠标按下的按钮是否为左键,如果是则输出一条调试信息。同时,我们还调用了父类的mousePressEvent函数,以确保其他事件的正常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值