当前位置:网站首页>QT signal and slot connection (loose coupling)
QT signal and slot connection (loose coupling)
2022-07-26 20:30:00 【Domineering Xiao Ming】
GUI In the user interface , When a user operates a widget , Need other widgets to respond , The traditional way often uses callback( Callback mechanism ) To achieve . The so-called callback is to pass the function pointer as a parameter to another function in advance , Then call the callback function in the appropriate place in the process of function processing. The callback mechanism has two defects : The type is not safe , There is no guarantee that the correct parameters will be used in calling the function ; Strong coupling , The handler must know which callback function to call .Qt The signal and slot mechanism is type safe , loose coupling , More flexible , More convenient .
Signal and slot (Signal & Slot) yes Qt Fundamentals of programming , It's also Qt A big innovation of . Because of the programming mechanism of signal and slot , stay Qt It becomes more intuitive and simple when dealing with the interactive operation of various components of the interface .
The signal (Signal) It's an event launched in a particular situation , for example PushButton The most common signal is emitted when the mouse clicks clicked() The signal . Use of transmitted signal Qt Of emit keyword .QT Of signals Keyword indicates entering the signal declaration area , Then you can declare your signal .
Put three controls on the dialog window , When clicking the corresponding control, the DeBug Output “XXX come on. ” word .

Slot (Slot) Is a function of the response to the signal . Slot is a function , With general C++ The function is the same , Can be declared in any part of the class (public、private or protected), Can have any parameter , It can also be called directly . The difference between slot function and general function is : Slot functions can be associated with a signal , When the signal is transmitted , The associated slot function is automatically executed .
We can add slot methods to the control by right clicking . You can also press on_ Object name _ Manually add slot method in the format of event name , The slot method added in this format can be automatically connected with Emitted when clicked clicked() The signal Connect .
private slots:
void on_whBtn_clicked();
void on_zgBtn_clicked();
void on_sjBtn_clicked(); // Auto connect on_ Object name _ Signal name keyword signals Define a signal , When the slot method clicked by the control executes, we let it send this signal .
signals:
void Commeon(QString& str); // Signals cannot and need not be realized , And the return value is voidUse emit Keywords send signals .
void MainWindow::on_whBtn_clicked()
{
QString str=" Come on, Wuhan ";
emit Commeon(str);
}
void MainWindow::on_zgBtn_clicked()
{
QString str=" Go China ";
emit Commeon(str);
}
void MainWindow::on_sjBtn_clicked()
{
QString str=" Let the world go ";
emit Commeon(str);
}Let's take another slot method , Used to correlate with the above signal ; The slot method will be a CString stay DeBug Print out
private slots:
void CommeonGuys(QString& str);
void MainWindow::CommeonGuys(QString& str)
{
qDebug()<<str;
}Bind the signal to the slot method newly defined above in the constructor
connect(this,SIGNAL(Commeon(QString&)),this,SLOT(CommeonGuys(QString&)));The signal is associated with the slot by QObject::connect() Function implementation :
QMetaObject::Connection QObject::connect(
const QObject *sender, // Signal sender
const char *signal, // Transmitted signal
const QObject *receiver, // The receiver of the signal
const char *method, // A string that represents the way the signal is connected , It can be a slot or a signal
Qt::ConnectionType type = Qt::AutoConnection // How to connect , Default auto connect
)Common formats :connect(sender, SIGNAL(signal()), receiver, SLOT(slot()));
Signal usage SIGNAL, For slots, use SLOT, Use them to surround the prototype of the function . Be careful connect The method is SIGNAL() And SLOT() when , there Function prototypes can only write types , Cannot have any parameter name , Otherwise, the connection will fail .
At the same time, the signal is connected with the slot :
- Qt::AutoConnection:( Default connection mode ) Automatic mode , The system automatically selects the connection mode .
- Qt::DirectConnection: Direct way , When the signal is transmitted , Call slot now .
- Qt::QueuedConnection: Queue mode , When the signal is transmitted, an event is generated and enters the queue , The slot can only call... When the event is processed .
- Qt::BlockQueuedConnection: Blocking queue mode , When the signal is transmitted, an event is generated and enters the queue , Then the current thread enters the blocking state , Until the event is handled , If the receiver is in the thread sending the signal , Then the program will deadlock , Therefore, this connection method is only used for multithreading .
You can also not use SIGNAL and SLOT, Take the address directly .
connect(this,&MainWindow::Common,this,&MainWindow::CommonGuys);The signal can be regarded as a special function , Need to be in parentheses , With parameters , The signal does not need to be realized and cannot be realized . Slot function requires parentheses , When there are parameters, you also need to specify the parameters . When the signal and slot function have parameters , stay connect() In the function , Specify the type of parameter . The parameters of the signal shall be consistent with the parameter list of the slot , More than slot parameters are allowed . If there are no matches or too few parameters , There will be compilation errors or running errors . In classes that use signals and slots , You must add macros to the class definition Q_OBJECT. When a signal is transmitted , The slot function associated with it is usually executed immediately , Just like calling a function normally . Only when all slot functions associated with the signal are executed , Will execute the code behind the transmitting signal .
边栏推荐
- 软件测试-开发提测内容规范(项目提测模板)
- C # convert PDF files into pictures
- YGG 与 AMGI 的旗舰 NFT 项目 My Pet Hooligan 合作进入 The Rabbit Hole
- GBase学习-安装GBase 8a MPP Cluster V95
- STM32F103有源蜂鸣器驱动程序
- The first training course was a perfect success (๑ㅂ•) و*
- numpy.zeros_ like
- How to praise others gracefully? Try these methods
- numpy.put()
- Software testing - development test content specification (project test template)
猜你喜欢

Servlet

数字化工厂有哪些关键技术

Exchange 2010 SSL证书安装文档

.net GC workflow

MPLS 多协议标签交换技术

Solve attributeerror: module 'win32com.gen_ py. 00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu

The first training course was a perfect success (๑ㅂ•) و*

How can small companies break through with small and beautiful products?

gospel! Wechat personal official account can be renamed!

正则表达式
随机推荐
this指向,最简单的规则记住它
Task 1 report
解决IBGP的水平分割和BGP选路原则
The Sandbox 和艺术家 Alec Monopoly 达成合作
There is an Oolong incident in open source. Maybe you don't know these five open source protocols
任务二 kaggle糖尿病检测
静音台式风扇芯片-DLTAP703SD-杰力科创
一文读懂 Kubernetes的四种服务类型!
Read the high-performance queue channel in.Net
Exchange 2010 SSL证书安装文档
Silent desktop fan chip dltap703sd Jericho
Small scenes bring great improvement! Baidu PaddlePaddle easydl helps AI upgrade of manufacturing assembly line
STM32F103 active buzzer driver
Week 6 Convolutional Neural Networks (CNNs)
UE5编辑器Slate快速入门【开篇】
vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
[record of question brushing] 22. bracket generation
同花顺靠谱吗?我刚开始学习理财,开证券账户安全吗?
What functions does the medical live broadcast platform need
ES6 method & Class array into real array & method of judging array