官术网_书友最值得收藏!

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:

Connecting two signals

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.

主站蜘蛛池模板: 浠水县| 始兴县| 罗山县| 罗山县| 昭觉县| 铁力市| 神农架林区| 马关县| 松桃| 汽车| 绿春县| 华安县| 上蔡县| 姜堰市| 灌南县| 土默特右旗| 祁门县| 虹口区| 大城县| 通城县| 永吉县| 巴中市| 温宿县| 苍山县| 合水县| 五原县| 丁青县| 朔州市| 夏津县| 金湖县| 常德市| 峨山| 北宁市| 那坡县| 师宗县| 腾冲县| 仙桃市| 浮梁县| 昔阳县| 大邑县| 东海县|