QGraphicsScene ignores Stretches of Grid Layout

233 Views Asked by At

I have a Widget that divides into two subwidgets. One shows a picture, the other one offers options to edit the picture. The picture subwidget should take 5 times the horizontal space than the edit subwidget. I specify this constraint in a Grid Layout. However, the constraint is ignored as soon as I add a QGraphicsScene (and View) to the edit subwidget.

I tried several size policies, but none of them did what I wanted, however I am not too experienced using those.

class MapWidget(QWidget):
""" Widget that renders the map and displays properties """
    def __init__(self, main_gui, parent=None):
        super().__init__(parent=parent)
        self.main_gui = main_gui

        # (Re-)Build the ui
        self.layout = QGridLayout()
        self.setLayout(self.layout)

        self.map_scene = QGraphicsScene()
        self.map_scene_view = QGraphicsView()
        self.map_scene_view.setViewport(QGLWidget())
        self.map_scene_view.setScene(self.map_scene)
        self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)

        blocks_container = QVBoxLayout()
        self.layout.addLayout(blocks_container, 1, 2, 1, 1)

        """
        Here I add other widgets to the blocks_container, 
        however none of those causes problems
        """

        # As soon as I also add this group, both widgets have the same size 
        group_blocks = QGroupBox('Blocks')
        self.blocks_scene = QGraphicsScene()
        self.blocks_scene_view = QGraphicsView()
        self.blocks_scene_view.setViewport(QGLWidget())
        self.blocks_scene_view.setScene(self.blocks_scene)
        group_blocks_layout = QHBoxLayout()
        group_blocks.setLayout(group_blocks_layout)
        group_blocks_layout.addWidget(self.blocks_scene_view)
        blocks_container.addWidget(group_blocks)

Instead of having the two widgets with ratios 5/6 and 1/6, both widgets share the same size, which makes the picture look small and the edit widget too big. Also, once I remove the 'group_blocks' widget, the edit subwidget has its proper size again. For simplicity I therefore excluded all the other widgets it contains.

1

There are 1 best solutions below

1
On BEST ANSWER

Try it and adjust as you need:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtOpenGL  import *

class MapWidget(QWidget):
    """ Widget that renders the map and displays properties """

    def __init__(self, main_gui="???", parent=None):          # main_gui="???"
        super().__init__(parent=parent)
        self.main_gui = main_gui

        # (Re-)Build the ui
        self.layout = QGridLayout()
        self.setLayout(self.layout)

        self.map_scene = QGraphicsScene()
        self.map_scene_view = QGraphicsView()
        self.map_scene_view.setViewport(QGLWidget())
        self.map_scene_view.setScene(self.map_scene)

#        self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)
        self.layout.addWidget(self.map_scene_view, 1, 1, 5, 5)

        blocks_container = QVBoxLayout()

#        self.layout.addLayout(blocks_container, 1, 2, 1, 1)
        self.layout.addLayout(blocks_container, 1, 6, 5, 1)

        self.layout.setColumnMinimumWidth(1, 320)              # +++
        self.layout.setRowMinimumHeight(1, 200)                # +++
        self.layout.setColumnStretch(1, 5)                     # +++
        self.layout.setColumnStretch(6, 1)                     # +++

        """
        Here I add other widgets to the blocks_container, 
        however none of those causes problems
        """

        # As soon as I also add this group, both widgets have the same size 
        group_blocks = QGroupBox('Blocks')
        self.blocks_scene = QGraphicsScene()
        self.blocks_scene_view = QGraphicsView()
        self.blocks_scene_view.setViewport(QGLWidget())
        self.blocks_scene_view.setScene(self.blocks_scene)
        group_blocks_layout = QHBoxLayout()
        group_blocks.setLayout(group_blocks_layout)
        group_blocks_layout.addWidget(self.blocks_scene_view)
        blocks_container.addWidget(group_blocks)


if __name__=="__main__":
    app = QApplication(sys.argv)
    myapp = MapWidget()
    myapp.show()
    sys.exit(app.exec_())

enter image description here