当前位置:网站首页>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
- 程序猿学英语-Learning C
- sqli-labs(POST类型注入)
- 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
- 文件上传-upload-labs
- gocv图片裁剪并展示
- Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
- KubeSphere 虚拟化 KSV 安装体验
- HCIA - application layer
- Linux安装Oracle Database 19c
猜你喜欢

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

Smart agriculture solutions smart agriculture system development

Sqli labs level 12

IP protocol and IP address

File upload Labs

OpenShift 部署应用

C language custom type enumeration, Union (clever use of enumeration, calculation of union size)

Nacos download, start and configure MySQL database

Minecraft群組服開服

Use C language to receive JSON strings
随机推荐
Causes of laptop jam
sqli-labs第8关(布尔盲注)
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
File upload Labs
How to apply for a secondary domain name?
Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Matlab - autres
How to uninstall SQL Server cleanly
[blackmail virus data recovery] suffix Crylock blackmail virus
OpenShift构建镜像
实现双向链表(带傀儡节点)
Linux二进制安装Oracle Database 19c
Googlenet network explanation and model building
路由基础—动态路由
Sqli labs level 1
Matlab-其它
群辉 NAS 配置 iSCSI 存储
File upload and download performance test based on the locust framework
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