为什么QgraphicsGridlayout不dane the Row ander Row和列放置在场景上的项目?

发布于 2025-01-26 17:14:45 字数 795 浏览 1 评论 0 原文

到目前为止,我有以下代码:

 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手柄放置不应该吗?

So far, I have the following code:

 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)

For some reason those to equal pixmaps are put onto each other. While I want then next to each other.

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)

This code over worked, but shouldn't QGraphicsGridLayout handle placement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-02-02 17:14:45

qgraphicsscene.additem()代码>无

您不会将PixMap项目添加到Qgraphicswidget,而是将它们直接添加到场景中。另外, first parameter 在qgraphicswidget的构造中> parent ,所以无论如何它都不有效;结果是您实际上在布局中添加了空的qgraphicswidgets。

让我们通过遵循执行顺序分析问题所在的前两行,这是问题所在:

QGraphicsWidget(self.scene.addItem(QGraphicsPixmapItem(self.pixmap)))
                                   ^^^^^^^^^^^^^^^^^^^(^^^^^^^^^^^)
                ^^^^^^^^^^^^^^^^^^(               1.               )
^^^^^^^^^^^^^^^(         2.                                         )
      3.

请记住,使用“嵌套”函数调用意味着 ofter call始终使用 Inner 呼叫,就像带有括号的数学表达式一样,出于明显的原因,首先执行 inner 调用:您无法在外括号内求解表达式,直到您评估结果内部括号。
在上面的情况下, 3 将使用 2 的返回值,该值将使用 1 的返回值执行。

命令 当前参数 结果 返回对象
1。 qgraphicsPixMapItem(self.pixmap) qpixmap 创建一个项目,将显示给定的PixMap qgraphicsPixMapiTem
2。 self.self.self.self.self.self.-scene.additem(...) qgraphicsPixMapItem实例 将给定项目添加到场景 none
3。 qgraphicsWidget(...) 创建 empty qgraphicswidget没有父母 qgraphicswidget实例

上述结果是 p1 p1 p2 是空的Qgraphicswidget项目添加到布局中,而PixMap项目则直接添加到场景中,这就是为什么它们看起来重叠的原因。这两行本可以是这样写的,给出相同的结果:

self.scene.addPixmap(self.pixmap)
self.scene.addPixmap(self.pixmap)
p1 = QGraphicsWidget()
p2 = QGraphicsWidget()

现在,问题是只能将qgraphicslayoutitem子类添加到qgraphicslayout中,而您不仅可以添加标准的qgraphicsItem,因此可能的解决方案是创建基本的qgraphicslayoutememlayoutemememlayoutemem。如文档

class PixmapLayoutItem(QGraphicsLayoutItem):
    def __init__(self, pixmap, parent=None):
        super().__init__(parent)
        self.pixmapItem = QGraphicsPixmapItem(pixmap)
        self.setGraphicsItem(self.pixmapItem)

    def sizeHint(self, which, constraint=None):
        return self.pixmapItem.boundingRect().size()

    def setGeometry(self, geometry):
        self.pixmapItem.setPos(geometry.topLeft())


# ...

p1 = PixmapLayoutItem(self.pixmap)
p2 = PixmapLayoutItem(self.pixmap)
self.grid.addItem(p1, 0, 0)
self.grid.addItem(p2, 0, 1)
f = QGraphicsWidget()
f.setLayout(self.grid)
self.scene.addItem(f)

请注意,上述子类是极其基础的,并且确实 考虑嵌入式项目的任何转换:如果要支持这些项目(例如缩放或旋转),则必须实现同时实现 sizehint()< /code>和 setGeometry()相应。

QGraphicsScene.addItem() returns None.

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:

QGraphicsWidget(self.scene.addItem(QGraphicsPixmapItem(self.pixmap)))
                                   ^^^^^^^^^^^^^^^^^^^(^^^^^^^^^^^)
                ^^^^^^^^^^^^^^^^^^(               1.               )
^^^^^^^^^^^^^^^(         2.                                         )
      3.

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 of 2, which will be executed with the return value of 1.

command current argument result returned object
1. QGraphicsPixmapItem(self.pixmap) QPixmap create an item that will display the given pixmap an instance of QGraphicsPixmapItem
2. self.scene.addItem(...) QGraphicsPixmapItem instance adds the given item to the scene None
3. QGraphicsWidget(...) None create an empty QGraphicsWidget without parents QGraphicsWidget instance

The result of the above is that both p1 and p2 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:

self.scene.addPixmap(self.pixmap)
self.scene.addPixmap(self.pixmap)
p1 = QGraphicsWidget()
p2 = QGraphicsWidget()

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.

class PixmapLayoutItem(QGraphicsLayoutItem):
    def __init__(self, pixmap, parent=None):
        super().__init__(parent)
        self.pixmapItem = QGraphicsPixmapItem(pixmap)
        self.setGraphicsItem(self.pixmapItem)

    def sizeHint(self, which, constraint=None):
        return self.pixmapItem.boundingRect().size()

    def setGeometry(self, geometry):
        self.pixmapItem.setPos(geometry.topLeft())


# ...

p1 = PixmapLayoutItem(self.pixmap)
p2 = PixmapLayoutItem(self.pixmap)
self.grid.addItem(p1, 0, 0)
self.grid.addItem(p2, 0, 1)
f = QGraphicsWidget()
f.setLayout(self.grid)
self.scene.addItem(f)

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() and setGeometry() accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文