博客
关于我
Qt信号和槽--对象之间的通信
阅读量:166 次
发布时间:2019-02-28

本文共 1367 字,大约阅读时间需要 4 分钟。

Qt中最明显的特点就是采用信号槽机制,使得对象之间可以相互通信,传递消息,在前台界面开发中应用尤其广泛。

关联两个对象,要使用conncet函数,如下,sender发送一个signal信号,receiver就会执行method方法

QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const \QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)

举一个例子

头文件

#ifndef SIGNALSLOTDEMO_H#define SIGNALSLOTDEMO_H#include 
class SignalSlotDemo : public QObject{ Q_OBJECTpublic: explicit SignalSlotDemo(QObject *parent = 0); int getValue();signals: void changedValue(int newValue);public slots: void setValue(int value);private: int value;};#endif // SIGNALSLOTDEMO_H

源文件

#include "signalslotdemo.h"#include 
using namespace std;SignalSlotDemo::SignalSlotDemo(QObject *parent) : QObject(parent),value(){}int SignalSlotDemo::getValue(){ return this->value;}void SignalSlotDemo::setValue(int value){ this->value = value; emit changedValue(this->value);}

测试文件

#include 
#include
using namespace std;#include "signalslotdemo.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); SignalSlotDemo demo1; SignalSlotDemo demo2; QObject::connect(&demo1, SIGNAL(changedValue(int)), &demo2, SLOT(setValue(int))); demo1.setValue(200); cout<
<

demo1 在调用自己的成员方法setValue的时候,会发射一个changedValue信号(携带变量value),这时候demo2的setValue就会被调用了

转载地址:http://uzfc.baihongyu.com/

你可能感兴趣的文章
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx 301跳转
查看>>
nginx 403 forbidden
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>