Efficiency of Qt Signals and Slots

2019-07-12 11:03发布

问题:

I was browsing the methods inside of QMainWindow and noticed that some parts (such as resizeEvent and winEvent) are not implemented as signals but rather you have to inherit this class to be able to override them.

My question is, how efficient are signals and slots and would it be possible to implement these types of functions as signals from which other classes can subscribe to. For instance, inside of a high performance game engine.

回答1:

From what I recall, Trolltech stated that a signal/slot call is about 10 times slower than a virtual call. You should be able to easily process tens, if not hundreds of thousands signals per second.



回答2:

Signal and slots are designed so that different objects can communicate, both objects being under your control.

The events you mention involve only one object at a time, which gets directly notified by the event loop. There is no need for signals or slots here.

Note that you can emit a signal inside an event member function if you want to. This is often the right way to react to an event. This way, other objects can subscribe to something.



回答3:

derive from QMainWindow, reimplement resize() and emit() signals from there, if you want to react on such signals at other places.