当前位置:网站首页>Connect function and disconnect function of QT
Connect function and disconnect function of QT
2022-07-02 08:49:00 【Code pirate captain】
Qt Of connect Functions and disconnect function
connect How to use
qt Of connect function , Signal and slot mechanism , yes QObject One of the core parts of , Use connect function , It's easy to do Qt Data transfer between objects , All inheritance QObject Objects defined by classes , You can use connect Function to pass messages ( data ), It is qt A great artifact of .
connect The ways of using it are 4 Kind of , Let's introduce them one by one .
One 、QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const
// The first one is connect Using examples
connect(this,SIGNAL(objectNameChanged(const QString &)),SLOT(slotObjectNameChanged(const QString &)),Qt::AutoConnection);
1) The pointer of the sending signal and the pointer of the receiving signal in the function are the same pointer , The essence of this function is to call the mode 2 Methods .
Two 、QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
// The second kind connect Using examples
connect(ui->pushButton_Object,SIGNAL(pressed()),this,SLOT(slotSetObjectName()),Qt::AutoConnection);
1) The first way is a special form of this way ;
2)connect Back to QMetaObject::Connection, This data type can be seen as bool Data type of , if connect The connection of function definition is correct , The return value is true, Otherwise false; also , Use disconnect(QMetaObject::Connection&) You can disconnect this connect function .
3、 ... and 、QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
// The third kind of connect Using examples
connect(this,&MainWinddow::objectNameChanged,this,&MainWinddow::slotObjectNameChanged,Qt::AutoConnection);
1) such connect The use of function does not restrict the data type of signal transmission , For the case of overloaded signals and slot functions , It will cause confusion of the compiler , therefore , In classes that contain overloaded signals or slot functions , This is not recommended .
Four 、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 Medium lambda Function definition connect function .
disconnect How to use
disconnect() Yes 3 Usage , Its prototype is as follows :
bool QObject::disconnect(const QObject * sender, const char * signal, const QObject * receiver, const char * method)
1. Disconnect from myObject The connection between the signal of an object and other objects , After using myObject The sent signal has no corresponding slot function to respond
disconnect(myObject, 0, 0, 0);
// or
myObject->disconnect();
2. Disconnect from myObject Object's mySignal() Connection between signal and other objects , After using myObject issue mySignal() The signal has no corresponding slot function to respond
disconnect(myObject, SIGNAL(mySignal()), 0, 0);
// or
myObject->disconnect(SIGNAL(mySignal()));
3. Disconnect from myObject Object and the myReceiver Connections between objects , After using myObject issue mySignal() The signal myReceiver The corresponding slot function responds
disconnect(myObject, 0, myReceiver, 0);
// or
myObject->disconnect(myReceiver);
Be careful :
1、0 Represents any signal or receiver object ;
2、const QObject * sender It can't be 0.
边栏推荐
- C#钉钉开发:取得所有员工通讯录和发送工作通知
- File upload Labs
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- Minecraft空岛服开服
- Qunhui NAS configuring iSCSI storage
- 用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
- Finishing the interview essentials of secsha system!!!
- 图像变换,转置
- STM32 new project (refer to punctual atom)
- IP protocol and IP address
猜你喜欢
Openfeign facile à utiliser
Finishing the interview essentials of secsha system!!!
Minecraft模组服开服
ICMP Protocol
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
Honeypot attack and defense drill landing application scheme
commands out of sync. did you run multiple statements at once
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
STM32 new project (refer to punctual atom)
File upload and download performance test based on the locust framework
随机推荐
Sqli labs level 1
Minecraft module service opening
Finishing the interview essentials of secsha system!!!
Flex layout
gocv拆分颜色通道
Web security -- core defense mechanism
Sqli labs level 8 (Boolean blind note)
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
群辉 NAS 配置 iSCSI 存储
Routing foundation - dynamic routing
Makefile基本原理
Openshift deployment application
C# 高德地图 根据经纬度获取地址
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Tensorflow2 keras 分类模型
Minecraft group service opening
Kubernetes deploys Loki logging system
用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
Pointer initialization
Realize bidirectional linked list (with puppet node)