Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MainWindow resize should give priority resize to DockWidget over other widgets

MainWindow resize should give priority resize to DockWidget over other widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keren M
    wrote on last edited by
    #1

    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_())
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Keren M
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved