QDockWidget QSizePolicy to get side dock to maintain it's sizeHint unless manually resized?

224 Views Asked by At

In the example below, the behavior I want is when resizing the QMainWindow it should give as much space as possible to the left widget and keep the right widget at a width of 200px (the sizeHint), but I don't want to use the Fixed sizePolicy because I want the user to be able to resize the right dock manually if desired.

Minimum only has the GrowFlag which says "The widget can be expanded, but there is no advantage to it being larger" and Expanding has the ExpandFlag which says "The widget can make use of extra space, so it should get as much space as possible".... but when I resize the window the right widget is immediately enlarged instead of the left widget getting the extra space. Why is this happening?

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
app = QtWidgets.QApplication([])

main = QtWidgets.QMainWindow()

class MyWidget(QtWidgets.QPushButton):
    def __init__(self,name, w,h):
        super(MyWidget, self).__init__(None)
        self.w = w
        self.h = h

    def sizeHint(self) -> QtCore.QSize:
        return QtCore.QSize(self.w,self.h)


widget1 = MyWidget("ONE",600,200)
widget2 = MyWidget("ONE",200,200)

dock1 = QtWidgets.QDockWidget()
dock2 = QtWidgets.QDockWidget()

dock1.setWidget(widget1)
dock2.setWidget(widget2)

widget1.setSizePolicy(QtWidgets.QSizePolicy.Expanding,QtWidgets.QSizePolicy.Expanding)
widget2.setSizePolicy(QtWidgets.QSizePolicy.Minimum,QtWidgets.QSizePolicy.Minimum)

main.addDockWidget(Qt.LeftDockWidgetArea, dock1)
main.addDockWidget(Qt.RightDockWidgetArea, dock2)

main.show()

app.exec_()

https://www.screencast.com/t/Eo8qZvrd

0

There are 0 best solutions below