Adding signals/slots (QObject) to QGraphicsItem: performance hit?(向 QGraphicsItem 添加信号/插槽(QObject):性能受到影响?)
问题描述
我想向 QGraphicsItem 添加信号/插槽,以便我可以从另一个线程访问 QGraphicsItemObjects.我知道有两个选项:使用 QGraphicsObject 或从 QObject 和 QGraphicsItem 继承.
I want to add signals/slots to a QGraphicsItem so I can reach QGraphicsItemObjects from another thread. There are two options that I know of: use QGraphicsObject or inherit from QObject and QGraphicsItem.
这被认为是缓慢的.根据 this answer on stackoverflow QGraphicsObjects 很慢,因为它们执行.当我查看 QGraphicsObjects 的源代码时,我可以看到根据对对象所做的更改发出了很多信号.对我来说,这似乎是 QGraphicsObjects 缓慢的一个合理的论点,但我认为这种性能下降(如果真的是)可以通过第二种解决方案来避免.
This is assumed to be slow. According to this answer on stackoverflow QGraphicsObjects are slow because of their implementation. When I look in the source of QGraphicsObjects I can see a lot of signals being emitted according to changes made to the object. To me this seems a plausible argument for why QGraphicsObjects are slow, but I think this performance hit (if it really is one) can be avoided by the second solution.
当构造一个继承自 QObject 和 QGraphicsItem 的类时,您似乎获得了 QGraphicsObject 最有趣的特性减去性能损失:您可以在类中定义插槽并发出信号,但您不继承默认实现的 QGraphicsObject 会在您可能不感兴趣的更改时不断发出信号.您现在可以发出信号,但不必担心为您不关心的事情发出信号(更改的 x 值会发出信号)在 QGraphicsObject 中,但不在此解决方案中).
When constructing a class that inherits from QObject and QGraphicsItem it seems that you get the most interesting feature of QGraphicsObject minus the performance hit: you are able to define slots and emit signals in your class but you don't inherit the default implementation of QGraphicsObject that would constantly emit signals on changes you might not be interested in. You are now able to emit signals but don't have to worry about signals being emitted for things you don't care about (x value that changes emits a signal in QGraphicsObject but not in this solution).
- QGraphicsObjects 真的比 QGraphicsItems 慢吗?
- 如果是,是不是因为实现会发出信号(并且发出信号是一个性能受到很大影响)?
- 如果是这样,第二种解决方案(多重继承)是否避免了这种惩罚?
推荐答案
此线程 建议另一种选择:创建一个 QObject 子类来代表您的 QGraphicsItems 发出信号.
This thread suggests another option: Create a QObject subclass to emit signals on behalf of your QGraphicsItems.
如果你有很多 QGraphicsItems 可以共享一个 QObject,那么这将比让每个 QGraphicsItem 继承 QObject 更轻量级.
If you have many QGraphicsItems that can share a single QObject, then this will be lighterweight than having each QGraphicsItem inherit QObject.
这篇关于向 QGraphicsItem 添加信号/插槽(QObject):性能受到影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 QGraphicsItem 添加信号/插槽(QObject):性能受到影响


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09