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):性能受到影响
基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01