QGraphicsWidget 与 QGraphicsLayoutItem
我第一次使用 QGraphicsView/Scene 。这是在 PyQt 中,但除了 Python 与多重继承不同之外,我认为这是一个通用的 Qt 问题。
我首先创建了几个 QGraphicsItem 重载作为构建块。一旦我让它们按照我想要的方式工作,我想尝试将它们组合起来,但令人不快地惊讶地发现我无法在 QGraphicsLayouts 中使用它们。这是因为 QGraphicsLayouts 采用从 QGraphicsLayoutItem 继承的项目,而 QGraphicsItems 又由 QGraphicsLayoutItems 继承,而 QGraphicsLayoutItems 又由 QGraphicsWidgets 继承。
QGraphicsItem/QGraphicsWidget 有一个 GraphicsItem 属性,但是查看代码,我认为我无法将我的 Item 分配给该属性并让它们正常工作。我确实找到了这个例子,但奇怪的是它有从这两个类继承的示例。让我很困惑。
因此,我试图找到让我的项目在布局中工作的最简单方法。有没有比重写和继承这两个类之一更简单的方法来做到这一点?
第二个问题,是否有一个经验法则告诉你什么时候应该从 QGraphicsWidget 继承,什么时候应该从 QGraphicsLayoutItem 继承?
额外感谢解释何时使用 sizeHint 与boundingRect。
感谢帮助, 布雷特
I'm working with QGraphicsView/Scene for the first time. This is in PyQt, but except for the fact that Python is different with multiple inheritance, I think this is a generic Qt question.
I started out by creating a couple of QGraphicsItem overloads as building blocks. Once I had those working the way I wanted, I wanted to try to combine them and was unpleasantly surprised to find that I couldn't use the in QGraphicsLayouts. This is due to the fact the QGraphicsLayouts take items that inherit from QGraphicsLayoutItem, and QGraphicsItems are inherited by QGraphicsLayoutItems, which are in turn inherited by QGraphicsWidgets.
There is a graphicsItem property of QGraphicsItem/QGraphicsWidget, but looking at the code, I don't think I can assign my Item's to this property and have them work properly. I did find this example, but strangely enough it has examples that inherit from both classes. Pretty confusing to me.
So I'm trying to find the easiest way to get my Items working in Layouts. Is there an easier way to do this than rewriting and inheriting from one of this two classes?
Secondary question, is there a rule of thumb for when you should inherit from QGraphicsWidget vs when you should inherit from QGraphicsLayoutItem?
Extra credit for explaining when sizeHint vs. boundingRect are used.
Appreciate the help,
Brett
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让项目在布局中工作的最简单方法是将继承更改为 QGraphicsWidget 并重写 setGeometry() 和 sizeHint() 方法。应该是一个简单的更改,因为您的项目仍然是通过继承祖先的 QGraphicsItems。
Qt Graphics View 框架被设计为尽可能轻量级。因此,组装具有不同功能的物品有多种选择。如果布局中项目的大小不是问题,您可以从 QGraphicsWidget 继承。否则,继承自 QGraphicsLayoutItem(除非您需要 QGraphicsWidget 的额外功能)。由于您无法从 PyQt 类进行多重继承,因此您必须使用组合来创建由 QGraphicsLayoutItem 控制的项目,如您引用的示例中所示。
场景使用boundingRect()方法来管理项目。布局使用 sizeHint() 方法来确定布局项的大小。场景使用 shape() 方法来更精确地确定项目的位置(用于碰撞、命中测试等)。
The easiest way to get items working in layouts is to change the inheritance to QGraphicsWidget and override the setGeometry() and sizeHint() methods. Should be a simple change since your items will still be QGraphicsItems through the inheritance ancestry.
The Qt Graphics View framework is designed to be as lightweight as possible. Thus there are many choices for assembling items with different capabilities. If the sizes of items to be in layouts are not a concern, you can inherit from QGraphicsWidget. Otherwise, inherit from QGraphicsLayoutItem (unless you need the extra capabilities of QGraphicsWidget). Since you can't multiply inherit from PyQt classes, you'll have to use composition for creating an item controlled by the QGraphicsLayoutItem, like in the example you referenced.
The boundingRect() method is used by the scene to manage items. The sizeHint() method is used by the layouts to determine the size of layout items. The shape() method is used by the scene to more precisely determine the location of items (for collisions, hit tests, etc).