如何使用 Boost::Signal 实现类似 QT 的信号连接语法

发布于 2024-08-21 10:28:19 字数 439 浏览 3 评论 0原文

在 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。

In QT we can connect signals and slots using the following simple syntax:

connect(pObject1, signal1, pObject2, slot2)

For instance, one can write something like:

A a;
B b;    
connect(&a, SIGNAL(valueChanged(int)), &a, SLOT(setValue(int)));

With Boost::Signal the syntax we would write it this way:

A a;
B b;    
a.valueChanged.connect(boost::bind(&B::SetValue, &b, _1))

IMHO, the boost signal's syntax is more complicated. Is there a way to make the Boost::Signal's syntax more QT like.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-08-28 10:28:19

Qt 的问题是它在编译期间会经历代码生成阶段,而 Boost 无法做到这一点。这意味着 Qt 可以做一些非常聪明的语法事情,如果不经过类似的过程就无法复制这些事情。

引用维基百科

称为 moc,这是一个在 Qt 程序源上运行的工具。它将 C++ 代码中的某些宏解释为注释,并使用它们生成附加的 C++ 代码,其中包含有关程序中使用的类的“元信息”。 Qt 使用此元信息来提供 C++ 本身不可用的编程功能:信号/槽系统、内省和异步函数调用。

(我无法获取链接,但它是http://en.wikipedia.org/wiki/Qt_(framework)

编辑:我认为维基百科quote 很清楚,信号/槽系统是使用 moc 实现的。我非常怀疑是否有任何方法可以在不使用类似系统的情况下使用相同的语法。

The thing with Qt is that it goes through a code generation phase during compilation, that Boost can't do. That means that Qt can do some very clever syntactic things that can't be copied without going through a similar process.

To quote 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.

(I can't get the link to work, but it's http://en.wikipedia.org/wiki/Qt_(framework))

Edit: I think the Wikipedia quote is quite clear that the signal/slot system is implemented using the moc. I doubt very much that there's any way to use the same syntax without using a similar system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文