当前位置:网站首页>Signal and slot mechanism of QT learning
Signal and slot mechanism of QT learning
2022-07-28 16:43:00 【tyrocjl】
Concept
The signal and slot are Qt Define a communication mechanism in , Realize the interaction between objects ; When the state of the object changes , Will send a signal , The signal can be received by other objects , After receiving the signal, it will execute a specified operation function ( Slot ).Definition
class XX:public QObject{ Q_OBJECT //moc Metaobject compiler signals: void sig_func(...);// The signal public slots: void slot_func(...){ ...}// Slot };notes : Signal functions need only be declared , Cannot write definitions
notes : Slot function can be connected to a signal , The execution of slot functions can be triggered by signals ; In addition, the slot function can also be used as a general member function , Call directly .Signal and slot connection
QObject::connect( const QObject* sender,// Signal sending object const char* signal, // Signal function const QObject* receiver,// Receiving object of signal const char* method)// Slot functionSIGNAL( Signal function ( Parameter table ))// Convert the signal function to const char*
SLOT( Slot function ( Parameter table ))// Convert the slot function to const char*Case study : establish Qt Applications , It contains a label (QLabel) And a button (QPushButton), Click the button to close the label function .
#include <QApplication> #include <QLabel> #include <QPushButton> #include <QFont> #include <QDialog> int main(int argc,char** argv){ QApplication app(argc,argv); QDialog parent;// The parent window parent.resize(480,320); // Create button and label controls QPushButton button(" Turn off the tag ",&parent); QLabel label(" I'm the label ",&parent); // Resize and position button.move(50,200); label.move(50,50); button.resize(150,50); label.resize(150,50); // Set font size QFont font; font.setPointSize(20); parent.setFont(font); // Dynamically create button objects QPushButton* button2 = new QPushButton(" sign out ",&parent); button2->move(300,200); button2->resize(150,50); // Show parent window parent.show(); // Click the button to close the tab QObject::connect( &button,SIGNAL(clicked(void)), &label,SLOT(close(void))); // practice : Use new Operate to dynamically create a button object , Wen benxian // in " sign out ", Click this button to exit the application . QObject::connect( button2,SIGNAL(clicked(void)), //&app,SLOT(closeAllWindows(void))); &app,SLOT(quit(void))); //&parent,SLOT(close(void))); return app.exec(); }Syntax requirements for signal and slot connections
1) Signal and slot parameters shall be consistentQObject::connect(A,SIGNAL(sigfun(int)), B,SLOT(slotfun(int)));//ok QObject::connect(A,SIGNAL(sigfun(void)), B,SLOT(slotfun(int)));//error2) There can be default parameters
class B:public QObject{ Q_OBJECT public slots: void slotfun(int = 0){ } }; QObject::connect(A,SIGNAL(sigfun(void)), B,SLOT(slotfun(void)));//ok3) The parameters of the signal can be more than slots , Redundant parameters will be ignored
QObject::connect(A,SIGNAL(sigfun(int)),B,SLOT(slotfun(void)));//ok
4) A signal can be connected to multiple slots at the same time ( One to many )QObject::connect(A,SIGNAL(sigfun(int)),B1,SLOT(slotfun1(int)));QObject::connect(A,SIGNAL(sigfun(int)),B2,SLOT(slotfun2(int)));
notes : When A Sending signal ,B1 and B2 All slot functions of will be executed , But the execution sequence is uncertain under multithreading .
5) Multiple signals can be connected to the same slot at the same time ( How to 1)
QObject::connect(A1,SIGNAL(sigfun1(int)), B,SLOT(slotfun(int)));
QObject::connect(A2,SIGNAL(sigfun2(int)), B,SLOT(slotfun(int)));
notes : No matter what A1 still A2 Sending signal ,B All slot functions are executed
6) The two signals can be directly connected ( The signals are connected in series )QObject::connect(A1,SIGNAL(sigfun1(int)), A2,SIGNAL(sigfun2(int)));
A1–A2–>B
notes : If A1 Sending signal ,A2 The signal will also be sentCase study : Event synchronization , Implement slider (QSlider) And the value selection box (QSpinBox) Synchronous operation
1) slider :QSlider
QSlider( vertical / level , Parent window pointer );// Constructors
void setRange(int min,int max);// Set the sliding range
void valueChanged(int value);// The signal sent when the slider slides
void setValue(int value);// Set the current slider position slot function
2) Value selection box :QSpinBox
QSpinBox( Parent window pointer );// Constructors
void setRange(int min,int max);// Set the value range of the value selection box
void valueChanged(int value);// The signal sent when the value changes
void setValue(int value);// Set the current value slot function#include <QApplication> #include <QSlider>// slider #include <QSpinBox>// Value selection box #include <QDialog> #include <QFont> int main(int argc,char** argv){ QApplication app(argc,argv); // The parent window QDialog parent; parent.resize(480,320); QFont font; font.setPointSize(20); parent.setFont(font); // slider QSlider slider(Qt::Horizontal,&parent); slider.setRange(1,100); slider.move(100,50); // Value selection box QSpinBox spin(&parent); spin.setRange(1,100); spin.move(100,200); // Show parent window parent.show(); // Slide the slider , The value of the selection box changes QObject::connect( &slider,SIGNAL(valueChanged(int)), &spin,SLOT(setValue(int))); // The value of the selection box changes , The slider slides with it QObject::connect( &spin,SIGNAL(valueChanged(int)), &slider,SLOT(setValue(int))); return app.exec(); }
Note that the above code should be compiled according to qmake -project qmeke make …
边栏推荐
- nowcode-学会删除链表中重复元素两题(详解)
- ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- Sort 1-insert sort and Hill sort
- The little red book of accelerating investment, "rush to medical treatment"?
- el-input限制只能输入规定的数
- 大地坐标系转换火星坐标系
- Qt学习之安装
- Leetcode daily practice - 160. Cross linked list
- LwIP develops | socket | TCP | keepalive heartbeat mechanism
猜你喜欢

MySQL view event status statements and modification methods

栈的介绍与实现(详解)

每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...

Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi

Im im development optimization improves connection success rate, speed, etc

LwIP development | realize TCP server through socket

一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

Sort 4-heap sort and massive TOPK problem

LeetCode每日一练 —— 160. 相交链表
随机推荐
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
flashfxp 530 User cannot log in. ftp
排序5-计数排序
Automatically pack compressed backup download and delete bat script commands
Reset grafana login password to default password
优化Hypermesh脚本性能的几点建议
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
Splash (rendering JS service) introduction installation
Fx3 development board and schematic diagram
el-input限制只能输入规定的数
Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
解决uniapp等富文本图片宽度溢出
排序3-选择排序与归并排序(递归实现+非递归实现)
Design direction of daily development plan
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
PHP mb_substr 中文乱码
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
HDU1847解题思路
The little red book of accelerating investment, "rush to medical treatment"?