Important: Please read the Qt Code of Conduct -

MainWindow resize should give priority resize to DockWidget over other widgets

  • I have a main window containing several widgets, and a dock widget which has the main display. I wish that when resizing the main window, the dock widget's view should first of all resize (enlarge) proportionally, while the other widgets stay at a fixed size.
    But I also want to allow the user to resize the inner widgets manually, so I added them to a QSplitter (and did not force them to a fixed size).

    From my experience, I've managed to do this only when defining ALL widgets as dock widgets, and then they do resize proportionally with the main window. But as soon as I add any other widget, the latter will be the one resizing while the dock widgets remain at a fixed size. Anyways, I am not interested in making all my other widgets to be DockWidgets.

    So what do I do in order for the DockWidget to be the primary widget resizing upon window resize?

    Window before resize:

    Window after resize:

    What I want is for the green area to have grown, instead of the red area.
    The green area is part of the dock widget, while the red and blue are two widgets placed inside a splitter.

    If helpful, the code in PyQt5 (but same concepts apply to Qt in c++):
    import sys
    from PyQt5 import QtCore, QtWidgets

    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            self._splitter = QtWidgets.QSplitter(self)
            self._first_widget = QtWidgets.QTableView(self._splitter)
            self._first_widget.setStyleSheet('background-color: red')
            self._second_widget = QtWidgets.QLabel(self._splitter)
            self._second_widget.setStyleSheet('background-color: blue')
            self._inner_widget = QtWidgets.QFrame(self)
            self._inner_widget.setStyleSheet('background-color: green')
            self._dock_widget = QtWidgets.QDockWidget("Dock Widget", self)
            self.addDockWidget(QtCore.Qt.TopDockWidgetArea, self._dock_widget)
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        win = MainWindow()

  • Lifetime Qt Champion

    If thy are in same layout, you could use
    Stretch Factor to make green take more of the space available.

  • I tried this, and though it did fix the resizing issue, the dock widget is now not able to be moved at all. This is because when setting a dock widget to a layout, it becomes part of that layout and acts as a regular widget.

    So how do I set the stretch but without restricting the dock widget to a layout?

Log in to reply