Domanda

I am trying to create a simple application with PySide, but it seems that I didn't read the docs properly. That is my code:

from PySide.QtGui import *
from PySide.QtCore import Qt

class Window(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)

        self.scene = QGraphicsScene()
        self.view1 = QGraphicsView(self.scene, self)
        self.view2 = QGraphicsView(self.scene, self)

        self.gridLayout = QGridLayout()
        self.gridLayout.addWidget(self.view1, 0, 0, Qt.AlignLeft)
        self.gridLayout.addWidget(self.view2, 0, 1, Qt.AlignRight)
        self.gridLayout.setColumnMinimumWidth(0, 300)
        self.gridLayout.setColumnMinimumWidth(1, 300)
        self.setLayout(self.gridLayout)

        self.view1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.view2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.scene.addLine(0, 0, 1000, 1000)

if __name__ == "__main__":
    app = QApplication(())
    window = Window()
    window.showMaximized()
    app.exec_()

The code executes, but it should display a window with two QGraphicsViews, which should divide the window in half, but I only get one QGraphicsView in its minimum size. Can someone help me with this?

Thanks in advance.

È stato utile?

Soluzione

You need to create a central widget for a QMainWindow, and then set the layout on that. Simplifying your example:

from PySide.QtGui import *

class Window(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)

        self.scene = QGraphicsScene()
        self.view1 = QGraphicsView(self.scene, self)
        self.view2 = QGraphicsView(self.scene, self)

        self.view1.setFrameShape(QFrame.NoFrame)
        self.view2.setFrameShape(QFrame.NoFrame)

        widget = QWidget(self)
        layout = QGridLayout(widget)
        layout.addWidget(self.view1, 0, 0)
        layout.addWidget(self.view2, 0, 1)

        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        self.setCentralWidget(widget)

        self.scene.addLine(0, 0, 1000, 1000)

if __name__ == "__main__":
    app = QApplication(())
    window = Window()
    window.showMaximized()
    app.exec_()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top