当前位置:网站首页>Qt学习10 初探 Qt 中的消息处理
Qt学习10 初探 Qt 中的消息处理
2022-06-29 10:30:00 【一个小黑酱】
Qt学习10 初探 Qt 中的消息处理
Qt消息模型
- Qt封装了具体操作系统的消息机制
- Qt遵循经典的GUI消息驱动时间模型

思考
- Qt中如何表示用户消息
- Qt中如何映射用户消息到消息处理函数?
- Qt中消息映射需要遵循什么规则?
信号与槽
Qt中定义了与系统消息相关的概念
信号 ( Signal )
- 由操作系统产生的消息
槽 ( Slot )
- 程序中的消息处理函数
连接 ( Connect )
- 将系统消息绑定到消息处理函数
Qt中的消息处理机制

信号到槽的连接必须发生在两个Qt类对象之间
- Qt的核心 - QObject::connect 函数
bool connect(const QObject* sender, // 发送对象
const char* signal, // 消息名
const QObject* receiver, // 接收对象
const char* method, // 接受对象的成员函数
Qt::ConnectionType type=Qt::AutoConnection)
备注:在Qt中,消息用字符串进行描述;connect 函数在消息名和处理函数之间建立映射
Qt中的“新”关键字
SIGNAL
- 用于指定消息名
SLOT
- 用于指定消息处理函数名1
Q_OBJECT
- 所有自定义槽的类必须在类声明开始出加上Q_OBJECT
slot
- 用于在类中声明消息处理函数
自定义槽
- 只有OBject的子类才能自定义槽
- 定义操的类必须在声明的最开始处使用Q_OBJECT
- 类中声明槽时需要使用slots关键字
- 槽与所处理的信号在函数签名上必须一致
- SIGNAL和SLOT所指定的名称中:
- 可以包含参数类型
- 不能包含具体的参数名
- 槽函数中通过 sender()函数 可以获得信号发起的对象
计算器程序加上信号槽
下面的代码只有信号和槽添加的内容,之前重复的部分不再展示
/* QCalculatorUI.h */
// 之前代码中包含的头文件
class QCalculatorUI : public QWidget
{
Q_OBJECT // 使用信号槽必须添加的配置
private:
// 之前代码中的内容
private slots: // 自定义的槽函数
void onButtonClicked();
public:
// 之前代码中的内容
};
/* QCalculatorUI.cpp */
// 之前代码中的内容
bool QCalculatorUI::construct()
{
// 之前代码中的内容
for(int i=0; (i<4) && ret; i++) {
for(int j=0; (j<5) && ret; j++) {
m_buttons[i*5 + j] = new QPushButton(this);
if( m_buttons[i*5 + j] != NULL ) {
// 之前代码中的内容
connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked())); // 给每个按钮的点击事件添加上槽函数
}
else {
ret = false;
}
}
}
return ret;
}
// 按钮的点击事件自定义槽函数
void QCalculatorUI::onButtonClicked()
{
QPushButton* btn = (QPushButton*)sender(); // 通过sender()函数得知是哪个按钮被按下了
qDebug() << "onButtonClicked()";
qDebug() << btn->text();
}
// 之前代码中的内容
小贴士
解决经典问题:Object::connect: No such slot …
1.检查类是否继承于QObject
2.检查类声明的开始处是否添加Q_OBJECT
3.检查是否使用slots关键字进行槽声明
4.检查槽的名称是否乒协错误
5.重新执行qmake
小结
- 信号与槽是Qt中的核心机制
- 不同的Qt对象可以通过信号和槽进行通信
- 只有QObject的子类才能自定义信号和槽
- 使用信号和槽必须在声明的最开始处使用Q_OBJECT
- 信号与处理函数在函数签名上必须一致
边栏推荐
- Exclusive interview with CTO: the company has deepened the product layout and accelerated the technological innovation of domestic EDA
- 【无标题】我在密谋一件大事
- The use of variables in shell that you have to be familiar with
- math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
- ruoyi框架中添加sharding sphere5.0.0分表(通过spi添加自定义分表策略)
- Data analysis method and Thinking: funnel analysis
- The first "cyborg" in the world died, and he only transformed himself to "change his life against the sky"
- (JS) imitate the indexof method to find the position of a character in the string
- Nuc980 started successfully
- Online text filter less than specified length tool
猜你喜欢

关于IP定位查询接口的测评Ⅱ

Live broadcast by technical experts from China Kuwait Fangde: how to build a private cloud platform based on openstack and CEPH| Issue 27

Week 12 experiment -- implementation of VGA protocol based on FPGA

CTO专访:合见工软深化产品布局 加速国产EDA技术革新

影响LED封装散热主要因素有哪些?

非凸联合创始人李佐凡:将量化作为自己的终身事业

Pipeline aggregations pipeline aggregations - parent-2

Using EasyX configuration in clion

What are the main factors that affect the heat dissipation of LED packaging?

EasyDSS部署在C盘,录像回看无法正常播放该如何解决?
随机推荐
ModbusTCP协议WIFI无线学习型单路红外模块(圆壳版)
Bs-gx-017 online examination management system based on SSM
Doodle cloud development demo login
Today in history: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born
rxjs Observable 设计原理背后的 Pull 和 Push 思路
多线程实现客户端与服务端通信(初级版本)
Shell quotation marks and escape are rarely noticed, but they are often used in writing scripts
Nuc980 open source project 16- start from SPI flash (w25q128)
Evaluation of IP location query interface Ⅱ
Several types of MySQL index invalidation and their solutions
How to properly manage temporary files when writing shell scripts
(JS) isnan() method determines the reason why undefined is true
Nature | 全球海洋微生物组的生物合成潜力
(JS) pure function in array
Using EasyX configuration in clion
在日本的 IT 公司工作是怎样一番体验?
斐波那锲数列与冒泡排序法在C语言中的用法
Micro blog comment architecture design
Pipeline aggregations管道聚合-Sibling-1
Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)