Resizing Docked Windows (QDockWidget)

Last edited on

Overview

Docked windows, for example using the QDockWidget control, do not respond to MouseDrag or resizing the same as non-docked windows or widgets.

This article demonstrates how to resize a docked window using two different approaches (example included):

Resize Docked Window Using Mouse: Creating a MouseDrag action by combining MousePress and MouseRelease

# About Example
# Supply specific QDockWidget's symbolic or real name (in this sample application QDockWidget is ColorSwatch)
# change_* functions take three parameters: QDockWidget as o, resize value as xdiff, snooze in seconds (optional) as snoozeFactor

def main():
    startApplication("mainwindow")

    dockToResize = waitForObject("{name='%s Dock Widget' type='ColorSwatch' window=':Qt Main Window Demo_MainWindow'}" % i)

    change_height_on_top(dockToResize, -20)
    change_height_on_top(dockToResize, 20)
    change_height_on_bottom(dockToResize, -20)
    change_height_on_bottom(dockToResize, 20)
    change_width_on_left(dockToResize, -20)
    change_width_on_left(dockToResize, 20)
    change_width_on_right(dockToResize, 20)
    change_width_on_right(dockToResize, -20)

def change_height_on_top(o, xdiff, snoozeFactor = 0):
    snooze(snoozeFactor)
    mousePress(o, 50, -2, MouseButton.LeftButton)
    start = 0
    end = xdiff
    step = 1
    if xdiff < 0:
        step = -1
    for i in range(start, end, step):
        mouseMove(o, 50, -2 + i)
    mouseRelease()
    snooze(snoozeFactor)

def change_height_on_bottom(o, xdiff, snoozeFactor = 0):
    snooze(snoozeFactor)
    mousePress(o, 50, o.height + 2, MouseButton.LeftButton)
    start = 0
    end = xdiff
    step = 1
    if xdiff < 0:
        step = -1
    for i in range(start, end, step):
        mouseMove(o, 50, o.height + 2 + i)
    mouseRelease()
    snooze(snoozeFactor)

def change_width_on_left(o, xdiff, snoozeFactor = 0):
    snooze(snoozeFactor)
    mousePress(o, -3, 50, MouseButton.LeftButton)
    start = 0
    end = xdiff
    step = 1
    if xdiff < 0:
        step = -1
    for i in range(start, end, step):
        mouseMove(o, -3 + i, 50)
    mouseRelease()
    snooze(snoozeFactor)

def change_width_on_right(o, xdiff, snoozeFactor = 0):
    snooze(snoozeFactor)
    mousePress(o, o.width + 3, 50, MouseButton.LeftButton)
    start = 0
    end = xdiff
    step = 1
    if xdiff < 0:
        step = -1
    for i in range(start, end, step):
        mouseMove(o, o.width + 3 + i, 50)
    mouseRelease()
    snooze(snoozeFactor)
resizeQDockWidget.py

Resize Docked Window Using QWidget.resize()

def main():
    startApplication("mainwindow")

    myDockedWindow = waitForObject(":Qt Main Window Demo.White Dock Widget [*]_ColorSwatch")
    myDockedWindow.resize(500, 600)
resizeQDockWidget.py

Resize Docked Window (and maintain Dock min and max Height and Width)

def main():
    startApplication("mainwindow")
    resizeDockWidget(waitForObject(":Qt Main Window Demo.White Dock Widget [*]_ColorSwatch"), 500, 600)

def resizeDockWidget(widget, newWidth, newHeight):
    originalSize = widget.size
    originalMin = widget.minimumSize
    originalMax = widget.maximumSize

    widget.resize(newWidth,newHeight)

    if newWidth != originalSize.width:
        if widget.width > newWidth:
            widget.setMaximumWidth(newWidth)
        else:
            widget.setMinimumWidth(newWidth)

    if newHeight != originalSize.height:
        if widget.height > newHeight:
            widget.setMaximumHeight(newHeight)
        else:
            widget.setMinimumHeight(newHeight)

    widget.setMinimumWidth(originalMin.width)
    widget.setMinimumHeight(originalMin.height)
    widget.setMaximumWidth(originalMax.width)
    widget.setMaximumHeight(originalMax.height)
resizeQDockWidget2.py

Download Example Test Suite

suite_QDockWidgetPlus_py.zip