Qt signals and slots in different classes
2012-09-02 15:42
363 查看
Remember that connections are not between classes, but between instances.
If you emit a signal and expect connected slots to be called, it must be emitted on an instance on which the connection was made. That's your problem.
Assuming Y is a Singleton:
If you do
and
will not listen to it yet.
Apart from that, it would be best to do one of the following things, though you could not emit the signal in the constructor of Y with these approaches:
Use a third class Z that knows both X and Y, and do the connection there
Dependency Injection. That means X gets an instance of Y in its constructor:
Example for a Dependency Injection Constructor:
If you emit a signal and expect connected slots to be called, it must be emitted on an instance on which the connection was made. That's your problem.
Assuming Y is a Singleton:
If you do
connect( Y::getInstance(), ... )
and
Y::getInstance()does
new Y()at some point, then the constructor of Y is called before the connection is set up. Like that, the signal will be emitted but the slot
will not listen to it yet.
Apart from that, it would be best to do one of the following things, though you could not emit the signal in the constructor of Y with these approaches:
Use a third class Z that knows both X and Y, and do the connection there
Dependency Injection. That means X gets an instance of Y in its constructor:
Example for a Dependency Injection Constructor:
X::X( Y* const otherClass ) { connect( otherClass, SIGNAL( ... ), this, SLOT( ... ) }
相关文章推荐
- 深入信号和槽(Signals and Slots in Depth)
- QT signals and slots
- 深入信号和槽(Signals and Slots in Depth)
- 深入信号和槽(Signals and Slots in Depth)
- 2.2深入信号和槽(Signals and Slots in Depth)
- How Qt Signals and Slots Work - Qt的信号-槽机制是如何工作的
- Qt:信号与槽(Signals and Slots) 下
- Qt:信号与槽(Signals and Slots) 上
- 20 ways to debug Qt signals and slots
- QT signals and slots
- Qt:信号与槽(Signals and Slots) 下
- 深入信号和槽(Signals and Slots in Depth)
- 2.6 Qt提供的控件和对话框类(Built-in Widget and Dialog Classes)
- QT signals and slots
- QT Signals and Slots
- How Qt Signals and Slots Work
- 2.2深入信号和槽(Signals and Slots in Depth)
- 解析Qt的信号-槽机制是如何工作的 - How Qt Signals and Slots Work
- QT的信号和槽机制:Signals and Slots
- 解析Qt的信号-槽机制是如何工作的 - How Qt Signals and Slots Work