简体   繁体   English

Qt信号和时隙的效率

[英]Efficiency of Qt Signals and Slots

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. 我在浏览QMainWindow内的方法时,发现某些部分(例如resizeEvent和winEvent)没有实现为信号,但是您必须继承此类才能覆盖它们。

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. 例如,在高性能游戏引擎内部。

From what I recall, Trolltech stated that a signal/slot call is about 10 times slower than a virtual call. 我记得,Trolltech表示,信号/插槽呼叫比virtual呼叫慢大约10倍。 You should be able to easily process tens, if not hundreds of thousands signals per second. 您应该能够轻松处理每秒数十万个信号。

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. 这样,其他对象可以订阅某些内容。

如果您想在其他地方对此类信号做出反应,则从QMainWindow派生,重新实现resize()并从那里发出()信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM