为什么QgraphicsGridlayout不dane the Row ander Row和列放置在场景上的项目?
到目前为止,我有以下代码:
p1 = QGraphicsWidget(self.scene.addItem(QGraphicsPixmapItem(self.pixmap)))
p2 = QGraphicsWidget(self.scene.addItem(QGraphicsPixmapItem(self.pixmap)))
self.grid.addItem(pixmap, 0, 0)
self.grid.addItem(pixmap2, 0,1 )
f = QGraphicsWidget()
f.setLayout(self.grid)
self.scene.addItem(f)
出于某种原因,这些代码相等的像素彼此相等。虽然我想要然后彼此相邻。
w1 = self.scene.addItem(QGraphicsPixmapItem(self.pixmap))
w1.setPos(0,0)
pixmap = QGraphicsWidget(w1)
w2 = self.scene.addItem(QGraphicsPixmapItem(self.pixmap))
w2.setPos(200,0)
pixmap2 = QGraphicsWidget(w2)
self.grid.addItem(pixmap, 0, 0)
self.grid.addItem(pixmap2, 0,1 )
f = QGraphicsWidget()
f.setLayout(self.grid)
self.scene.addItem(f)
此代码过时了,但是QgraphicsGridlayout手柄放置不应该吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
qgraphicsscene.additem()代码>无
。您不会将PixMap项目添加到Qgraphicswidget,而是将它们直接添加到场景中。另外, first parameter 在qgraphicswidget的构造中> parent ,所以无论如何它都不有效;结果是您实际上在布局中添加了空的qgraphicswidgets。
让我们通过遵循执行顺序分析问题所在的前两行,这是问题所在:
请记住,使用“嵌套”函数调用意味着 ofter call始终使用 Inner 呼叫,就像带有括号的数学表达式一样,出于明显的原因,首先执行 inner 调用:您无法在外括号内求解表达式,直到您评估结果内部括号。
在上面的情况下,
3
将使用2
的返回值,该值将使用1
的返回值执行。qgraphicsPixMapItem(self.pixmap)
self.self.self.self.self.self.-scene.additem(...)
none
qgraphicsWidget(...)
上述结果是
p1 p1
和p2
是空的Qgraphicswidget项目添加到布局中,而PixMap项目则直接添加到场景中,这就是为什么它们看起来重叠的原因。这两行本可以是这样写的,给出相同的结果:现在,问题是只能将qgraphicslayoutitem子类添加到qgraphicslayout中,而您不仅可以添加标准的qgraphicsItem,因此可能的解决方案是创建基本的qgraphicslayoutememlayoutemememlayoutemem。如文档。
请注意,上述子类是极其基础的,并且确实 考虑嵌入式项目的任何转换:如果要支持这些项目(例如缩放或旋转),则必须实现同时实现
sizehint()< /code>和
setGeometry()
相应。QGraphicsScene.addItem()
returnsNone
.You're not adding the pixmap items to the QGraphicsWidget, you're directly adding them to the scene. Also, the first parameter in the constructor of QGraphicsWidget is a parent, so it wouldn't be valid anyway; the result is that you are actually adding empty QGraphicsWidgets to the layout.
Let's analyze those first two lines, which is where the problem resides, by following the order of execution:
Remember that using "nested" function calls means that the outer calls always use the returned values of the inner calls, exactly like math expressions with brackets, with inner calls being executed first for obvious reasons: you cannot solve the expressions within the outer parentheses until you evaluate the result the inner ones.
In the case above,
3
will use the return value of2
, which will be executed with the return value of1
.QGraphicsPixmapItem(self.pixmap)
self.scene.addItem(...)
None
QGraphicsWidget(...)
None
The result of the above is that both
p1
andp2
are empty QGraphicsWidget items that are added to the layout, while the pixmap items were directly added to the scene, which is why they appear overlapped. Those two lines could have been written like this, giving the same result:Now, the problem is that only QGraphicsLayoutItem subclasses can be added to a QGraphicsLayout, and you cannot just add a standard QGraphicsItem, so the possible solution is to create a basic QGraphicsLayoutItem subclass that will act as a "container" for the pixmap item, and implement the required functions, as explained in the documentation.
Note that the above subclass is extremely basic and does not consider any transformation of the embedded item: if you want to support those (like scaling or rotation), you must implement both
sizeHint()
andsetGeometry()
accordingly.