0

I have a problem layouting a PyQt window. I'm using a grid layout with one big QGLWidget and many smaller control components below it. When subclassing the QGLWidget I'm using the following code:

class GLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        self.parent = parent
        QtOpenGL.QGLWidget.__init__(self, parent)
        sizePol=QSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
        sizePol.setWidthForHeight(True)
        self.setSizePolicy(sizePol)

    def sizeHint(self):
        return QSize(300,450)

    def widthForHeight(self,height):
        return int(height/1.5)

I create the lyout with the following code:

class MainWidget(QtGui.QWidget): 
    def __init__(self, parent = None):
        super(MainWidget, self).__init__(parent)    
        self.ogl_widget = GLWidget(self)
        grid = QtGui.QGridLayout()
        grid.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
        grid.addWidget(self.ogl_widget, 0,0,1,6)
        #add other control components 
        <add LineEdits and Labels>
        <their size policy is vertical: fixed, horizontal: expanding>

However, there is no change in the size of the QGLWidget. It always fills the remaining space with the resulting arbitrary aspect ratio. What do I have to add / change to fix this issue?

Cookie
  • 678
  • 1
  • 11
  • 22

1 Answers1

0

After some more tests I found a solution:

First point is, that widthForHeight is not called at all but heightForWidth is, so I changed it implementing heightForWidth instead and setting sizePol.setHeightForWidth(True).

Second point is, that the Grid Layout overrides the preferred height if there is not enough space. In my use case I can just place the QGLWidget in a QScrollArea and the aspect ratio is right.

Cookie
  • 678
  • 1
  • 11
  • 22