Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:
    0_1522232279067_d539de18-23ac-481c-b337-80950aed37ff-image.png

    Window after resize:
    0_1522232322028_f38c1cae-f173-48f3-9c1f-f06f794b70e6-image.png

    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):
            QtWidgets.QMainWindow.__init__(self)
    
            self._splitter = QtWidgets.QSplitter(self)
            self._splitter.setOrientation(QtCore.Qt.Vertical)
            self.setCentralWidget(self._splitter)
    
            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._dock_widget.setFloating(False)
            self._dock_widget.setWidget(self._inner_widget)
    
            self.addDockWidget(QtCore.Qt.TopDockWidgetArea, self._dock_widget)
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())

  • Lifetime Qt Champion

    Hi
    If thy are in same layout, you could use
    Stretch Factor to make green take more of the space available.
    http://doc.qt.io/qt-5/layout.html



  • 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