当前位置:网站首页>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。
边栏推荐
- Chrome debugging
- 双向链表的实现(双向链表与单向链表的简单区别联系和实现)
- gocv边界填充
- Web security -- core defense mechanism
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- kubeadm部署kubernetes v1.23.5集群
- The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
- Generate database documents with one click, which can be called swagger in the database industry
- [dynamic planning] p4170: coloring (interval DP)
- Linux安装Oracle Database 19c RAC
猜你喜欢
What is SQL injection
Synchronize files using unison
Kubesphere virtualization KSV installation experience
Sentinel easy to use
Nacos download, start and configure MySQL database
Qunhui NAS configuring iSCSI storage
Kubedm deploys kubernetes v1.23.5 cluster
Zipkin is easy to use
OpenFeign 簡單使用
Rotating linked list (illustration)
随机推荐
Sqli labs level 1
IP protocol and IP address
整理秒杀系统的面试必备!!!
STM32 new project (refer to punctual atom)
【无标题】
Qunhui NAS configuring iSCSI storage
How to build the alliance chain? How much is the development of the alliance chain
Jz-061-serialized binary tree
Linux二进制安装Oracle Database 19c
[blackmail virus data recovery] suffix Rook3 blackmail virus
Sentinel 简单使用
Sqli labs Level 2
Openfeign is easy to use
Dip1000 implicitly tagged with fields
Causes of laptop jam
Gateway is easy to use
Dip1000 runaway
Minecraft group service opening
Tensorflow2 keras 分类模型
ICMP Protocol