Important: Please read the Qt Code of Conduct -

How to resize QMainWindow after removing all DockWidgets?

  • Hi,

    I'm trying to make an application consisting of a QMainWindow, the central widget of which is a QToolBar (it may not be usual, but for my purpose the toolbar's well suited). Docks are allowed below only. I added a QDockWidget to it, and a QAction on the QToolBar toggles the QDockWidget on and off with removeDockWidget() and restoreDockWidget().

    The default size of the QMainWindow is 800 by 24, QToolBar's MaximumHeight is set to 24 too. Right after the removeDockWidget() is called, QMainWindow's geometry is set back to (0,0,800,24) with setGeometry().

    What I want to achieve is to resize the QMainWindow's height to 24 when the DockWidget's removed. The setGeometry() seems to work since width and position change accordingly, but funnily enough, the height doesn't budge. And that's my problem really :)

    What's the matter?

    Oh, and "here's a screen-cast too":

    Thanks :)

  • Try calling "QLayout::activate() ": on the layout after removing the dock widget. That does the trick for me in the example below.

    Does this give you the result you want?

    #include <QtGui>

    class MainWindow : public QMainWindow
    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    dock1 = new QDockWidget(this);

    QPushButton *button = new QPushButton("Click me", this);

    addDockWidget(Qt::BottomDockWidgetArea, dock1);
    connect(button, SIGNAL(toggled(bool)), this, SLOT(toggleDockWidget(bool)));

    QSize sizeHint() const
    return QSize(800, 24);

    QSize minimumSizeHint() const
    return QSize(800, 24);

    public slots:

    void toggleDockWidget(bool test)
    if (test) {
    setGeometry(100,100, 800, 24);
    } else {
    } test = !test;

    QToolBar *toolBar;
    QDockWidget *dock1;


    #include "main.moc"

    int main(int argc, char** argv)
    QApplication app(argc, argv);
    MainWindow window;
    return app.exec();


  • Hi there Sigrid and thanks for your reply.

    I'm using PyQt4, and I am not used to C at all. I've translated what you tell me to calling self.layout().acivate() from the QMainWindow() class.

    That didn't change anything though (in appearance at least) :(

    I've tried to reproduce the same GUI without using the QMainWindow, but rather using QWidgets. After removing the child QWidget (playing the role of the dock), calling a adjustSize() reduced the size of the parent to its bare minimum.

    When using QMainWindow however, the adjustSize() seems to affect the width, but not the height of the QMainWindow, there is still about 30px height which I can reduce manually with the mouse.

    So then it seems to be QMainWindow specific. Would that have to do with some area reserved for the dock?


  • Unfortunately, I don't know PyQt, so I can't help you out with problems you encounter there. Does the example I pasted above work for you though? Does it work for you in PyQt if you port my example above to PyQt so that it is set up exactly the same way? Regarding the QMainWindow, then the layout needs to be redone so that it does not account for the removed QDockWidget. As opposed to QWidget, QMainWindow has an internal layout that it is necessary to redo.

  • OK, so this internal layout is the QMainWindow().layout() which I can activate() ?

    I've tried to implement the layout().activate() using PyQt in the toggling method. That didn't help. I perhaps have to redo it from scratch trying to reproduce your code in python and see how that goes.

    Otherwise I guess I'd have to improvise with Widgets.

    Thanks :)

  • Yes, you can get hold of the internal layout of the QMainWindow by calling layout() on it.

Log in to reply