当前位置:网站首页>Qt的connect函数和disconnect函数
Qt的connect函数和disconnect函数
2022-07-02 06:30:00 【代码海贼团船长】
Qt的connect函数和disconnect函数
connect的使用方式
qt的connect函数,信号与槽的机制,是QObject的核心部分之一,使用connect函数,可以方便进行Qt对象与对象之间数据传递,所有继承QObject类的类定义的对象,都可以使用connect函数传递消息(数据),简直是qt的一大神器。
connect的使用方式有4种,下面我们来逐个介绍。
一、QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const
// 第一种connect使用实例
connect(this,SIGNAL(objectNameChanged(const QString &)),SLOT(slotObjectNameChanged(const QString &)),Qt::AutoConnection);
1)函数中的发送信号的指针和接收信号的指针为同一指针,此函数的本质也是调用了方式2的方法。
二、QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
// 第二种connect使用实例
connect(ui->pushButton_Object,SIGNAL(pressed()),this,SLOT(slotSetObjectName()),Qt::AutoConnection);
1)第一种方式是这种方式的特殊形式;
2)connect返回的QMetaObject::Connection,这个数据类型可以看做bool的数据类型,若connect函数定义的连接正确,返回值为true,否则为false;并且,使用disconnect(QMetaObject::Connection&)可以断开此connect函数。
三、QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
// 第三种connect使用实例
connect(this,&MainWinddow::objectNameChanged,this,&MainWinddow::slotObjectNameChanged,Qt::AutoConnection);
1)这种connect函数的使用方式没有约束信号传送的数据类型,对于重载信号和槽函数的情况,会造成编译器的困惑,所以,在含有重载信号或者槽函数的类中,不建议使用这种方式。
四、QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
QByteArray page = ...;
QTcpSocket *socket = new QTcpSocket;
socket->connectToHost("qt-project.org", 80);
QObject::connect(socket, &QTcpSocket::connected, this, [=] () {
socket->write("GET " + page + "\r\n");
}, Qt::AutoConnection);
1)c++11中的lambda函数定义connect函数。
disconnect的使用方式
disconnect()有3种用法,其原型如下:
bool QObject::disconnect(const QObject * sender, const char * signal, const QObject * receiver, const char * method)
1.断开与myObject对象的信号与其他对象间的连接,使用后myObject发出的信号没有对应的槽函数进行响应
disconnect(myObject, 0, 0, 0);
// or
myObject->disconnect();
2.断开与myObject对象的mySignal()信号与其他对象间的连接,使用后myObject发出mySignal()信号没有对应的槽函数进行响应
disconnect(myObject, SIGNAL(mySignal()), 0, 0);
// or
myObject->disconnect(SIGNAL(mySignal()));
3.断开与myObject对象与myReceiver对象间的连接,使用后myObject发出mySignal()信号myReceiver对应的槽函数进行响应
disconnect(myObject, 0, myReceiver, 0);
// or
myObject->disconnect(myReceiver);
注意:
1、0表示任意的信号或者接收者对象;
2、const QObject * sender不能是0。
边栏推荐
- In depth understanding of prototype drawings
- 判断是否是数独
- Short video with goods source code, double-click to zoom in when watching the video
- HackTheBox-Gunship
- Rotating linked list (illustration)
- Chrome debugging
- 2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
- Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
- Learning C
- Minecraft group service opening
猜你喜欢

Minecraft群組服開服

Sqli labs Level 2

Simple implementation scheme of transcoding and streaming (I)

Sqli labs level 8 (Boolean blind note)

sqli-labs第2关

k8s入门:Helm 构建 MySQL

c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)

Minecraft插件服开服

Nacos 下载启动、配置 MySQL 数据库

Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
随机推荐
Chrome debugging
实现双向链表(带傀儡节点)
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
OpenFeign 簡單使用
Simple implementation scheme of transcoding and streaming (I)
Benefits of ufcs of D
HCIA - data link layer
OpenShift构建镜像
Use Wireshark to grab TCP three handshakes
ARP及ARP欺骗
File upload and download performance test based on the locust framework
【无标题】
OpenShift 部署应用
Luogu greedy part of the backpack line segment covers the queue to receive water
CarSim learning experience - rough translation 1
kubeadm部署kubernetes v1.23.5集群
Tensorflow2 keras 分类模型
Minecraft空岛服开服
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)