简体   繁体   English

Qt中的信号和插槽究竟是什么?

[英]What exactly are signals and slots in Qt?

I know how they work conceptually, but how are signals and slots implemented in the Qt framework? 我知道它们在概念上是如何工作的,但是Qt框架中的信号和插槽是如何实现的? Qt Creator treats them as keywords, but are they simply a set of macros, or is a special pre-processor required before these source files can be compiled? Qt Creator将它们视为关键字,但它们只是一组宏,还是在编译这些源文件之前需要的特殊预处理器?

In other words, if I use Qt's signal/slot features in my code, can I easily compile it on any C++ compiler? 换句话说,如果我在代码中使用Qt的信号/插槽功能,我可以在任何C ++编译器上轻松编译吗?

Many features of Qt, including signals, require preprocessing the source using the Qt Meta-Object Compiler (MOC) . Qt的许多功能,包括信号,都需要使用Qt元对象编译器(MOC)对源进行预处理。

If you use Qt's signals feature, you can compile your source code on any compiler and platform that is supported by Qt. 如果您使用Qt的信号功能,您可以在Qt支持的任何编译器和平台上编译源代码。 If you need to compile on a platform not supported by Qt, you may be out of luck or at least in for a lot of fun integration work (this is true of any library or framework, of course). 如果你需要在Qt不支持的平台上编译,你可能会运气不好或至少在很多有趣的集成工作中(当然,任何库或框架都是如此)。

If you are looking for a cross-platform signals implementation, you might also consider Boost.Signals . 如果您正在寻找跨平台信号实现,您可能还会考虑Boost.Signals

Yes, signal and slots in Qt are implemented as macros. 是的,Qt中的信号和插槽是作为宏实现的。 However MOC (Meta object compiler) is used to interpret it. 但是,MOC(元对象编译器)用于解释它。 MOC is a part of Qt. MOC是Qt的一部分。

If you want to use signals/slots in non Qt projects you might use Boost implementation ( http://www.boost.org/doc/libs/1_39_0/doc/html/signals.html ). 如果要在非Qt项目中使用信号/插槽,可以使用Boost实现( http://www.boost.org/doc/libs/1_39_0/doc/html/signals.html )。

Other implementations are listed on wiki page: http://en.wikipedia.org/wiki/Signals_and_slots 其他实现在wiki页面上列出: http//en.wikipedia.org/wiki/Signals_and_slots

You can use any compiler Qt supports. 您可以使用任何编译器Qt支持。 Which are the most commonly used ones, don't know if there is any widespread compiler not supported. 哪些是最常用的,不知道是否有任何广泛的编译器不支持。 However, the signal/slot/moc is not the limiting factor here. 但是,信号/ slot / moc不是限制因素。 Signal/slots require the moc executable to be called on header files declaring QObjects and the generated code to be compiled and linked. 信号/槽需要在头文件上调用moc可执行文件,声明QObjects并生成代码以进行编译和链接。 So integrating the moc call in your existing build system might be the biggest issue. 因此,在现有构建系统中集成moc调用可能是最大的问题。 If you start from scratch, I'd suggest to go for qmake, or cmake. 如果你从头开始,我建议去qmake或cmake。

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

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