Qt Layout on QMainWindow

Milan picture Milan · Oct 2, 2009 · Viewed 73.6k times · Source

I designed a QMainWindow with QtCreator's designer. It consists of the default central widget (a QWidget) which contains a QVBoxLayout with all the other widgets in it. Now everything I want, is that the QVBoxLayout automatically occupies the whole central widgets rectangle space.

How can I do this? I didn't find any usable property neither in the central widgets properties nor the QVBoxLayout's ones.

Answer

Jaime Ivan Cervantes picture Jaime Ivan Cervantes · Sep 17, 2012

If you want to do it with code instead of using QtCreator, you could set the layout in a QWidget and then set the QWidget as the central widget of the main window like this:

#include <QtGui>
#include <QWidget>
#include <QHBoxLayout>
#include "mainwindow.h"

MainWindow::MainWindow() {  

        // Set layout
        QHBoxLayout *layout = new QHBoxLayout;
        layout->addWidget(myWidget1);
        layout->addWidget(myWidget2);

        // Set layout in QWidget
        QWidget *window = new QWidget();
        window->setLayout(layout);

        // Set QWidget as the central layout of the main window
        setCentralWidget(window);

}