简体   繁体   English

QGraphicsGridLayout在PySide中有效,但在PyQt4中无效

[英]QGraphicsGridLayout works in PySide but not PyQt4

I've been trying for hours to get QGraphicsGridLayout to work with PyQt4 . 我一直在努力几个小时,以使QGraphicsGridLayoutPyQt4 I have PySide installed so I switched the imports to that for a quick check and it worked as expected! 我已经安装了PySide所以我将导入切换到该导入以进行快速检查,并且按预期运行! For the code below, when PySide is used, the paint method on the RectangleWidget is called as expected but when you use PyQt4 the paint method is never called. 对于下面的代码,使用PySide时, PySide预期方式调用RectangleWidget上的paint方法,但是当您使用PyQt4 ,则永远不会调用paint方法。

I know that the RectangleWidget should override some more virtual methods for a proper implementation but I was stripping things out to try and get the minimal amount of code to narrow down the problem. 我知道RectangleWidget应该重写一些更多的虚拟方法以实现正确的实现,但是我正在剥离一些东西以尝试并获取最少的代码来缩小问题的范围。

from PySide import QtGui, QtCore
# from PyQt4 import QtGui, QtCore


class RectangleWidget(QtGui.QGraphicsWidget):
    def __init__(self, rect, parent=None):
        super(RectangleWidget, self).__init__(parent)
        self.rect = rect

    def paint(self, painter, *args, **kwargs):
        print('Paint Called')
        painter.drawRect(self.rect)


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.central_widget = QtGui.QWidget(self)
        central_layout = QtGui.QHBoxLayout()
        self.central_widget.setLayout(central_layout)
        self.setCentralWidget(self.central_widget)
        self.resize(500, 500)

        self.view = QtGui.QGraphicsView()
        self.scene = QtGui.QGraphicsScene()
        self.view.setScene(self.scene)

        panel = QtGui.QGraphicsWidget()
        self.scene.addItem(panel)

        layout = QtGui.QGraphicsGridLayout()
        panel.setLayout(layout)

        for i in range(4):
            for j in range(4):
                rectangle = RectangleWidget(QtCore.QRectF(0, 0, 50, 50))
                layout.addItem(rectangle, i, j)

        central_layout.addWidget(self.view)

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    app.exec_()

Any help is appreciated! 任何帮助表示赞赏! I would like to maintain compatibility with both PyQt4 and PySide so continuing using PySide only is not really an ideal solution. 我想保持与PyQt4PySide兼容性,因此仅继续使用PySide并不是一个理想的解决方案。 Thanks 谢谢

The QGraphicsGridLayout takes ownership of the items added to it (see the docs for further details). QGraphicsGridLayout拥有添加到其中的项目的所有权(更多信息请参见文档 )。

In your example, it would seem that all the RectangleWidget items are in danger of being garbage-collected once they go out of scope. 在您的示例中,似乎所有RectangleWidget项目一旦超出范围,就有被垃圾回收的危险。 So if you give them a parent: 因此,如果您给他们父母:

    rectangle = RectangleWidget(QtCore.QRectF(0, 0, 50, 50), panel)

all should be well. 一切都应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM