向 QGraphicsItem 添加信号/插槽(QObject):性能受到影响?

2021-12-09 00:00:00 qt4 qt c++

我想向 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.

相关文章