- 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.
- Objective-C應(yīng)用開發(fā)全程實錄
- Learning Elixir
- 征服RIA
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- Unity Shader入門精要
- Access 2016數(shù)據(jù)庫管
- BIM概論及Revit精講
- Visual C#通用范例開發(fā)金典
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- 從零開始學(xué)C#
- Hands-On Kubernetes on Windows
- R語言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- Python開發(fā)基礎(chǔ)
- Penetration Testing with the Bash shell
- Java高級程序設(shè)計