博客
关于我
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/

你可能感兴趣的文章
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>