- Qt 5 Blueprints
- Symeon Huang
- 305字
- 2021-07-23 19:48:17
Connecting two signals
Due to the weak couplings of the Qt signals and slot mechanisms, it is viable to bind signals to each other. It may sound confusing, so let me draw a diagram to make it clear:

When an event triggers a specific signal, this emitted signal could be another event, which will emit another specific signal. It is not a very common practice, but it tends to be useful when you deal with some complex signals and slot connection networks, especially when tons of events lead to the emission of only a few signals. Although it definitely increases the complexity of the project, binding these signals could simplify the code a lot. Append the following statement to the construction function of MainWindow
:
connect(ui->bonjourButton, &QPushButton::clicked, ui->helloButton, &QPushButton::clicked);
You'll get two lines in a plain text edit after you click on the Bonjour button. The first line is Bonjour and the second one is Hello. Apparently, this is because we coupled the clicked signal of the Bonjour button with the clicked signal of the Hello button. The clicked signal of the latter has already been coupled with a slot, which results in the new text line, Hello. In fact, it has the same effect as the following statement:
connect(ui->bonjourButton, &QPushButton::clicked, [this](){ emit ui->helloButton->clicked(); });
Basically, connecting two signals is a simplified version of connecting a signal and a slot, while the slot is meant to emit another signal. As for priority, the slot(s) of the latter signal will be handled when the event loop is returned to the object.
However, it is impossible to connect two slots because the mechanism requires a signal while a slot is considered a receiver instead of a sender. Therefore, if you want to simplify the connection, just wrap these slots as one slot, which can be used for connections.
- Learn Blockchain Programming with JavaScript
- 高手是如何做產品設計的(全2冊)
- 自己動手寫搜索引擎
- C語言程序設計(第3版)
- YARN Essentials
- ASP.NET 3.5程序設計與項目實踐
- Spring+Spring MVC+MyBatis整合開發實戰
- Hands-On Full Stack Development with Go
- Learning Continuous Integration with TeamCity
- R用戶Python學習指南:數據科學方法
- Visual Basic程序設計習題與上機實踐
- Arduino可穿戴設備開發
- Magento 2 Beginners Guide
- OpenCV Android Programming By Example
- Clojure Polymorphism