7

I have an app with a lot of QDockWidgets and without central widget. I want to set some of those QDockWidgets initial size (size at application's start), but I don't want to limit their min/max sizes.

How to do it? For example, I need to set initial size 150x75 to one of them. I tried obvious methods (such as QWidget.Resize() relating to dock widget content), but it didn't work at all.

Here is a simplified model of situation:

from PyQt4 import QtCore, QtGui

app = QtGui.QApplication([''])

mw = QtGui.QMainWindow() # mw = MainWindow
mw.setCentralWidget(None)
mw.showMaximized()

mw.dockWdg1 = QtGui.QDockWidget(mw)
mw.content1 = QtGui.QTreeWidget()
mw.dockWdg1.setWidget(mw.content1)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(2), mw.dockWdg1)
mw.dockWdg1.setWindowTitle("1st dock widget")

mw.dockWdg2 = QtGui.QDockWidget(mw)
mw.content2 = QtGui.QTreeWidget()
mw.dockWdg2.setWidget(mw.content2)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(1), mw.dockWdg2)
mw.dockWdg2.setWindowTitle("2nd dock widget")

mw.dockWdg3 = QtGui.QDockWidget(mw)
mw.content3 = QtGui.QTreeWidget()
mw.dockWdg3.setWidget(mw.content3)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(1), mw.dockWdg3)
mw.dockWdg3.setWindowTitle("3rd dock widget")

mw.show()
app.exec_()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Bruno Gelb
  • 5,322
  • 8
  • 35
  • 50
  • 1
    `QMainWindow` must always have a central widget but you can try to set its size to 0. – alexisdm Dec 05 '12 at 02:33
  • @alexisdm, well, how to do that? Can you please post some code that can help me? Thank you! – Bruno Gelb Dec 05 '12 at 02:37
  • For example, with `mw.setCentralQWidget(QWidget())` and `mw.centralWidget().setFixedSize(0,0)`. [That question](http://stackoverflow.com/q/10067821/894321) shows the kind of problems you can have without setting the central widget. – alexisdm Dec 05 '12 at 13:01

4 Answers4

11

The dockwidgets will be incorporated into the layout of the main window, so any attempt to resize them will be ignored.

The standard workaround for this is to create a subclass of the content widget and reimplement its sizeHint:

class TreeWidget(QtGui.QTreeWidget):
    def sizeHint(self):
        return QtCore.QSize(150, 75)

mw.dockWdg2 = QtGui.QDockWidget(mw)
mw.content2 = TreeWidget()
mw.dockWdg2.setWidget(mw.content2)

However, this will only work to the extent that you also carefully manage the sizes of the other dockwidgets. And of course maximiizing the main window is also going to have a impact on the final outcome.

You might also want to consider using QMainWindow.saveState and QMainWindow.restoreState to manage the initial state of your dockwidgets.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

Use the fixedSize function, as setFixedWidth/setFixedHeight.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Cayan
  • 452
  • 4
  • 8
  • This constains the widget to exactly that size; the original poster wanted just the default size, not the minimum and maximum size to be set. – RolKau Aug 14 '15 at 13:37
0

I think reimplementing the sizeHint() function based on MainWindow's size is better than just returning a fixed size.

Source Code

// size hint
QSize RenderWindow::sizeHint() const
{
    float width = MainWindow::instance()->width() * 0.3f;
    float height = MainWindow::instance()->height() * 0.65f;
    return QSize( width, height);
}

enter image description here

anothernode
  • 5,100
  • 13
  • 43
  • 62
LIANG LIU
  • 81
  • 4
0

Anyone landing on this old question since Qt 5.6 should look at QMainWindow::resizeDocks()

https://doc.qt.io/qt-5/qmainwindow.html#resizeDocks

eg:

mw.resizeDocks({ mw.dockWdg1,  mw.dockWdg1 }, { 260, 540 }, Qt.Vertical);

(Not sure that's 100% valid Py, but you get the idea...)

Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22