当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
队列的基本概念介绍以及典型应用示例
IP协议与IP地址
Web技术发展史
HCIA—数据链路层
C call system sound beep~
C# 调用系统声音 嘀~
Pointer initialization
Luogu greedy part of the backpack line segment covers the queue to receive water
C nail development: obtain all employee address books and send work notices
Web安全--核心防御机制
File upload and download performance test based on the locust framework
Network security - summary and thinking of easy-to-use fuzzy tester
[blackmail virus data recovery] suffix Rook3 blackmail virus
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Mutex
Sqli labs (post type injection)
gocv拆分颜色通道
Linux二进制安装Oracle Database 19c
Minecraft插件服开服
HCIA—应用层









![[blackmail virus data recovery] suffix Rook3 blackmail virus](/img/46/debc848d17767d021f3f41924cccfe.jpg)