0

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>


eyllanesc
  • 235,170
  • 19
  • 170
  • 241
geodranic
  • 145
  • 9
  • 1
    `setExpanded` is undocumented (and frankly I'm a bit surprised that it's exposed to PyQt) and is called internally for other purposes (the behavior of a toolbar that exceedes the available space), so I would not suggest its use: I don't know on what platform (and style) you're testing, but I get unreliable behavior, especially using `adjustSize()`. Trying to meddle around with a QMainWindow layout is normally discouraged, as its behavior is completely internal and also deeply depends on the OS and style: while it *may* work on your computer, it's not guaranteed that it would on other's. – musicamante Nov 26 '21 at 13:11
  • 1
    Also, I get a warning when using `setExpanded`: `Trying to create a QVariant instance of QMetaType::Void type, an invalid QVariant will be constructed instead`. While not critical, that's not a good signal. – musicamante Nov 26 '21 at 13:14
  • @musicamante the findChild and setExpanded are only there as sort of a hacky solution to other odd behavior when undocking, where I had a right adjusted QAction that would disappear until the window was manually resized. Its Windows 10, python 3.9.7 – geodranic Nov 26 '21 at 14:08
  • I get what they do, that doesn't change the point: `setExpanded` is not an official and supported *public* function, and it's only used privately by the layout of QMainWindow (and for a different purpose). If you're doing it for *another* reason (what "odd behavior" are you referring to?), then you should try to explain that better, so that we can understand if it's the expected behavior, if there's a *proper* solution or even if it's a bug; a valid [mre] might also help. – musicamante Nov 26 '21 at 14:21
  • Adjusted my question with reproducible example – geodranic Nov 26 '21 at 14:55
  • As I was afraid of: I get unreliable behavior as soon as the dock is floating: the window gets resized, but the toolbar (and, actually, the whole layout) is not properly updated, so the window content gets partially hidden by the reduced window geometry. Can you explain what you're actually trying to achieve? – musicamante Nov 26 '21 at 18:39
  • I'm simply trying to remove the blank space when the dock is floating, and expand again when it's redocked – geodranic Nov 29 '21 at 11:05
  • 1
    Do you mean the blank space left by the dock, which is not filled by your frame? That has nothing to do with the dock, but the fact that you didn't specify a layout for the central widget. Right click on an empty space of the main window, and select a proper layout from the "Lay out" submenu. Then the contents of the window will automatically adapt when the dock becomes floating. – musicamante Nov 29 '21 at 11:11
  • I'll accept that answer - thanks. It was an oversight and I hadn't set a layout for the centralwidget – geodranic Dec 07 '21 at 14:20

0 Answers0