Please nominate your Qt Champions for 2021!

How to create central widget which does not expand on main window resize?

  • I have a main window with a central widget and a few dock widgets. I would like the central widget to maintain its size when the main window is resized. How can I achieve that? At present, when the main window is resized, the central widget gets resized while the dock widgets try to maintain their sizes. I even tried to set a fixed size for the central widget but even that does not help.
    I am using Qt 4.8.

  • you have to describe what happens to additional space.

    One of the possibility you have (dummy ) central widget which resizes, but such widget will have a child not in the layout.

  • Consider this example:

    #include <QtGui/QMdiArea>  
    int main(int argc, char** argv) 
        QApplication app (argc, argv);
        QMainWindow* w = new QMainWindow();
        QWidget *dummy = new QTextEdit();
        w->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        QDockWidget* consoleDock = new QDockWidget("console", w);
        consoleDock->setWidget(new QTextEdit());
        consoleDock->widget()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QDockWidget* gDock = new QDockWidget("graphics", w);
        gDock->setWidget(new QMdiArea());
        gDock->widget()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        w->addDockWidget(Qt::RightDockWidgetArea, gDock);
        w->addDockWidget(Qt::BottomDockWidgetArea, consoleDock);
        return 0;

    I want only the dock widgets marked graphics and console to expand when the main window is expanded. How can I achieve that?

  • All I say below depends on my understanding and my knowledge.
    Both might be failing in this case.

    If you want docked widgets expanding you need to resize them manually when window is resized.
    Normally dock widgets do not change their size, only central widget area does.

Log in to reply