In my current layout, I have a table in a dock widget that is initially docked (right side). I adjust the contents of the QMainWindow by connecting a function to topLevelChanged() to fill the blank space while floating
self.log_dock.topLevelChanged.connect(self.resize_dock)
def resize_dock(self):
layout = self.tb_utils.findChild(QtWidgets.QLayout)
layout.setExpanded(True)
self.adjustSize()
This works wonderfully - however; after doing so - I'm not entirely sure how to get it to redock to it's original position, or animate to expand the window again. If I manually adjust the QMainWindow slightly, it will allow the dock - or it can be moved into another dock area, and then moved back into position - but this is not ideal.
EDIT: New program using minimal reproducible example
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QLayout
import sys
gui = uic.loadUiType('dock_behavior.ui')[0]
class Window(QMainWindow, gui):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
self._connectEvents()
def _connectEvents(self):
self.dockWidget.topLevelChanged.connect(self.resize_dock)
self.show_dock.toggled.connect(self.enable_dock)
def resize_dock(self):
self.adjustSize()
layout = self.toolBar.findChild(QLayout)
layout.setExpanded(True)
def enable_dock(self):
self.dockWidget.toggleViewAction().trigger()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
The GUI as produced by Qt Designer
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>825</width>
<height>600</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>500</width>
<height>600</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>19</x>
<y>19</y>
<width>461</width>
<height>541</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>461</width>
<height>541</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>19</x>
<y>19</y>
<width>411</width>
<height>501</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout"/>
</widget>
</widget>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="show_dock"/>
</widget>
<widget class="QDockWidget" name="dockWidget">
<property name="minimumSize">
<size>
<width>291</width>
<height>0</height>
</size>
</property>
<attribute name="dockWidgetArea">
<number>2</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents"/>
</widget>
<action name="show_dock">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show / Hide Dock</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>