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