当前位置:网站首页>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。
边栏推荐
- 16: 00 interview, came out at 16:08, the question is really too
- ARP and ARP Spoofing
- Analysis of the use of comparable, comparator and clonable interfaces
- c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
- 2022 Heilongjiang latest food safety administrator simulation exam questions and answers
- ICMP协议
- STM32-新建工程(参考正点原子)
- First week of JS study
- 顺序表基本功能函数的实现
- Minecraft模组服开服
猜你喜欢

IP协议与IP地址

Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)

链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]

Smart agriculture solutions smart agriculture system development
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

kubeadm部署kubernetes v1.23.5集群

Kubesphere virtualization KSV installation experience

路由基础—动态路由

【无标题】
随机推荐
Sqli labs Level 2
D interface and domain problems
sqli-labs第12关
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
Service de groupe minecraft
Luogu greedy part of the backpack line segment covers the queue to receive water
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
Simple implementation scheme of transcoding and streaming (I)
gocv图片读取并展示
旋转链表(图解说明)
What is SQL injection
【无标题】
Nacos download, start and configure MySQL database
用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
Flex layout
Zipkin is easy to use
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
Gateway is easy to use
IP协议与IP地址