Qt如何将信号连接到具有最高优先级的槽(即使其他槽已提前连接)

huangapple go评论68阅读模式
英文:

Qt how to connect a signal to a slot with the highest priority (even if other slots are connected in advance)

问题

由于某种原因,我需要在Qt中首先调用稍后连接的槽。

connect(action, &QAction::triggered, obj1, &MyObject::slot1);
connect(action, &QAction::triggered, obj2, &MyObject::slot2);

我需要做什么来确保首先调用slot1

英文:

For some reason, I need a later connected slot to be called first in Qt.

connect(action, &QAction::triggered, obj1, &MyObject::slot1);
connect(action, &QAction::triggered, obj2, &MyObject::slot2);

What need I do to make the slot1 to be firstly called?

答案1

得分: 1

你需要一个中间的 QObject(你自己的子类)。它有一个传入的槽函数和两个(或更多)传出的信号,每个信号对应你需要的每个优先级。这些信号需要具有与原始信号相匹配的参数。

将原始信号连接到你为此目的创建的中间槽函数的实例上。然后该槽函数将按优先级顺序发射中间信号。你将你的原始接收槽函数连接到同一中间对象的这些中间信号上。

但是如果你有改变原始类的API的可能性,你可以直接将更高优先级的信号(首先发射的信号)添加到它。这样会简单得多。

英文:

You need an intermediate QObject (your own subclass). It has one incoming slot, and two (or more) outgoing signals, one signal for each priority you need. These need to have parameters matching the original signal.

Connect the original signal to this intermediate slot of an instance you create for the puroose. That slot will then emit the intermediate signals in priority order. You connect your original receiving slots to these intermediate signals of the same intermediate object.


But if you have a possibility to chance the API of the original class, you could add the higher priority signal (emitted first) directly to it. Much simpler.

huangapple
  • 本文由 发表于 2023年6月2日 00:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定