在 QT 中,我们可以使用以下简单语法连接信号和插槽:
connect(pObject1, signal1, pObject2, slot2)
例如,您可以编写如下内容:
A a;
B b;
connect(&a, SIGNAL(valueChanged(int)), &a, SLOT(setValue(int)));
使用 Boost::Signal 语法我们可以这样写:
A a;
B b;
a.valueChanged.connect(boost::bind(&B::SetValue, &b, _1))
恕我直言,boost 信号的语法更复杂。有没有办法让 Boost::Signal 的语法更像 QT。
最佳答案
Qt 的问题在于它在编译期间经历了代码生成阶段,这是 Boost 无法做到的。这意味着 Qt 可以做一些非常聪明的语法事情,如果不经过类似的过程就无法复制。
报价 Wikipedia :
Known as the moc, this is a tool that is run on the sources of a Qt program. It interprets certain macros from the C++ code as annotations, and uses them to generate additional C++ code with "Meta Information" about the classes used in the program. This meta information is used by Qt to provide programming features not available natively in C++: the signal/slot system, introspection and asynchronous function calls.
(我无法获得工作链接,但它是
http://en.wikipedia.org/wiki/Qt_(framework)
)编辑:我认为维基百科引用很清楚信号/插槽系统是使用 moc 实现的。我非常怀疑有没有办法在不使用类似系统的情况下使用相同的语法。
关于qt - 如何使用 Boost::Signal 实现类似 QT 的信号连接语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2279598/