当前位置:网站首页>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.
边栏推荐
猜你喜欢

Detailed explanation of NIN network

Application of kotlin - higher order function

C nail development: obtain all employee address books and send work notices

Gateway is easy to use

sqli-labs第2关

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away

队列的基本概念介绍以及典型应用示例

Qt——如何在QWidget中设置阴影效果

Openshift build image

Routing foundation - dynamic routing
随机推荐
寻找链表中值域最小的节点并移到链表的最前面
Linux二进制安装Oracle Database 19c
Qt的右键菜单
Makefile Fundamentals
Sqli labs level 12
Minecraft module service opening
Openshift deployment application
Minecraft插件服开服
Oracle related statistics
Hcia - Application Layer
OpenShift构建镜像
zipkin 简单使用
gocv图片裁剪并展示
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
HackTheBox-Gunship
Tensorflow2 keras 分类模型
STM32 new project (refer to punctual atom)
Analysis and solution of a classical Joseph problem
Function ‘ngram‘ is not defined
Installing Oracle database 19C RAC on Linux