当前位置:网站首页>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。
边栏推荐
- First week of JS study
- HackTheBox-Gunship
- Openshift build image
- Generate database documents with one click, which can be called swagger in the database industry
- Viewing JS array through V8
- Introduction to anti interception technology of wechat domain name
- When a custom exception encounters reflection
- Sqli labs Level 2
- 【无标题】
- Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

What is SQL injection

Smart agriculture solutions smart agriculture system development

Don't know mock test yet? An article to familiarize you with mock

Gateway 简单使用

File upload Labs

Jumping | Blue Bridge Cup
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]

C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)

Sqli labs level 12

Application of kotlin - higher order function
随机推荐
Rotating linked list (illustration)
群辉 NAS 配置 iSCSI 存储
判断是否是数独
Realization of basic function of sequence table
Smart agriculture solutions smart agriculture system development
Makefile基本原理
Use the kaggle training model and download your own training model
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
When a custom exception encounters reflection
Analysis of the use of comparable, comparator and clonable interfaces
Mutex
Viewing JS array through V8
Development of digital collection trading website development of metauniverse digital collection
How to apply for a secondary domain name?
KubeSphere 虚拟化 KSV 安装体验
Web技术发展史
Hcia - Application Layer
Openfeign facile à utiliser
STM32-新建工程(参考正点原子)
[blackmail virus data recovery] suffix Rook3 blackmail virus