当前位置:网站首页>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.
边栏推荐
- Solid principle: explanation and examples
- c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
- Linux安装Oracle Database 19c
- C nail development: obtain all employee address books and send work notices
- C call system sound beep~
- D interface and domain problems
- 文件上传-upload-labs
- Hcia - Application Layer
- 将一串数字顺序后移
- Analysis of the use of comparable, comparator and clonable interfaces
猜你喜欢
随机推荐
Network security - summary and thinking of easy-to-use fuzzy tester
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Makefile基本原理
Dip1000 runaway
Analysis and solution of a classical Joseph problem
File upload and download performance test based on the locust framework
Tcp/ip - transport layer
KubeSphere 虚拟化 KSV 安装体验
NPOI 导出Word 字号对应
Minecraft插件服开服
Nacos 下载启动、配置 MySQL 数据库
C# 高德地图 根据经纬度获取地址
什么是SQL注入
随笔:RGB图像颜色分离(附代码)
Web security -- core defense mechanism
OpenFeign 簡單使用
sqli-labs第1关
Solution of Xiaomi TV's inability to access computer shared files
Data asset management function









