I have placed 2 QDockWidgets in a QMainWindow, one on the left and one on the right.
Here is my code:
test_dock_widget.py
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QDockWidget, QWidget
class WindowMain(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.WindowType.WindowMinimizeButtonHint
| Qt.WindowType.WindowMaximizeButtonHint
| Qt.WindowType.WindowCloseButtonHint)
self.resize(800, 600)
self.setWindowTitle('Symptoms')
self.dock1 = QDockWidget('DockWidget1', self)
self.dock2 = QDockWidget('DockWidget2', self)
self.dock1.setWidget(QWidget())
self.dock1.widget().setStyleSheet('background: #FFC0C0')
self.dock2.setWidget(QWidget())
self.dock2.widget().setStyleSheet('background: #C0FFC0')
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.dock1)
self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.dock2)
def main():
app = QApplication(sys.argv)
wndMain = WindowMain()
wndMain.show()
app.exec_()
if __name__ == '__main__':
main()
The problem is that when the program runs, they seem to prefer to evenly distribute the entire width of QMainWindow. For example, every time I manually reduce the width of the left QDockWidget, maximize and restore the window, both QDockWidgets will again evenly distribute across the entire width of QMainWindow.
How can I avoid this situation?
I hope that the width allocation of these two QDockWidgets can be restored to what I adjusted before maximizing, but the result is that they are evenly distributed. I don't want to set a minimum or maximum width for any QDockWidget because users should be able to adjust them as they wish. I also don't want to set a fixed scaling ratio for these two QDockWidgets.
Thank you in advance!
Environment: Windows 11, Python 3, PyQt5
The space was evenly distributed between two docks because you placed two docks in different docking areas. You need to place them in the same docking area horizontally as in the following example. With this, the ratio of widths of the docks will be preserved when the window is resized, and the previous geometry will be restored after the window is maximized and restored to the normal state.
...
class WindowMain(QMainWindow):
def __init__(self):
super().__init__()
...
self.dock1 = QDockWidget('DockWidget1', self)
self.dock2 = QDockWidget('DockWidget2', self)
...
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.dock1, Qt.Horizontal)
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.dock2, Qt.Horizontal)
Now about preserving the widths of the docks when the window is resized, it's impossible to do it for both docks without a central widget of variable width. But it's possible to preserve the width of only one dock. For example, you can place two docks like the above example and additionally set the size policy of the left dock to Fixed
or Maximum
, as in the following example.
...
sp = self.dock1.sizePolicy()
sp.setHorizontalPolicy(QSizePolicy.Maximum)
self.dock1.setSizePolicy(sp)