問題描述
我想向 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):性能受到影響?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!