0

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
pyjamas
  • 4,608
  • 5
  • 38
  • 70
  • The behavior you're facing depends on lots of aspects, but the question is: there's no central widget set for the main window, is this by choice? Because as soon as you set one, the right dock widget will respect its size policy. – musicamante May 05 '21 at 17:02
  • @musicamante My main content is all inside tabified QDockWidgets because I want each module to be snappable and detachable. I don't want any central widget that cannot be moved or floated. This is what my actual project looks like: https://www.screencast.com/t/06uANvaa If there's no way to fix the sizing in my example with no central widget guess I could have the Filters as a QDockWidget and the other 4 tabs as docks inside an inner QMainWindow which I set as the central widget beside Filters on the main QMainWindow? – pyjamas May 05 '21 at 17:21
  • 1
    That would be difficult to achieve. QMainWindow layout management is pretty "closed", and there are lots of posts about dock management issues as its behavior only covers "standard usage" (wich also requires using a central widget). There's no easy solution: the default behavior partially ignores the size policy (more on this later[\*]), and there's also the issue that QSplitter has its own management of hints (and you cannot set your own QSplitter subclass to override it) which makes sense for general usage but creates unwanted behavior for specific cases like the one at hand – musicamante May 05 '21 at 17:44
  • 1
    Some attempts to work around it exist ([this](https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System) and [this](https://www.kdab.com/development-resources/qt-tools/kddockwidgets/)). I never tested them and AFAIK only the first has python bindings. Also [\*]consider that policy naming and descriptions are a bit confusing: `Minimum` doesn't mean that a widget will *never* be bigger than the hint. "no advantage to it being larger" means, more or less "it doesn't need more space if any other widget requires it (due to hint or expanding policy), but if there's space left it will use it. – musicamante May 05 '21 at 17:45

0 Answers0