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

LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数

Learn ABAQUS script programming script in an hour
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Using pyqt to design gui in ABAQUS

Headline article_ signature

Configure HyperMesh secondary development environment on vs Code

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

Several methods of HyperMesh running script files

LwIP development | realize TCP server through socket

Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
随机推荐
HyperMesh自动保存(增强版)插件使用说明
LwIP development | socket | TCP | client
Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
HM二次开发 - Data Names及其使用
laravel
日常开发方案设计指北
QML signal and handler event system
Qt学习第一天
SCI scientific paper writing Growth Camp (full version)
Some suggestions on optimizing HyperMesh script performance
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
Design direction of daily development plan
Asp.net large file block upload breakpoint resume demo
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
Qt学习之信号和槽机制
排序3-选择排序与归并排序(递归实现+非递归实现)
关于web对接针式打印机问题,Lodop使用
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
Headline article_ signature