41

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.

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
Milan
  • 3,342
  • 3
  • 31
  • 40

4 Answers4

82

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);

}
NorthCat
  • 9,643
  • 16
  • 47
  • 50
Jaime Ivan Cervantes
  • 3,579
  • 1
  • 40
  • 38
  • 4
    Thank you so much! But I don't get it ... what's the reasoning behind the introduced need to create a new widget? Wouldn't it have been more intuitive to just allow the layout to be applied to the main window? – User1291 Feb 04 '17 at 15:48
  • I faced same issue. Before coming here, Logically I was trying to setLayout on QMainWindow but didn't work. Using QWidget it worked. – Swapnil Dec 16 '18 at 14:11
  • 2
    If `MainWindow` inherits form `QMainWindow` then you can just call `centralWidget()->setLayout(layout);`. I found this [here](https://stackoverflow.com/questions/33251047/qt-setlayout-and-setcentralwidget) – Swapnil Dec 17 '18 at 04:52
  • My concern is that the [Qt docs](https://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework) say `Note: Creating a main window without a central widget is not supported.` That being said, it compiles and runs for me... But I wish Qt was a little more clear about this "requirement". – cdgraham Jan 26 '20 at 00:03
  • How do you `delete()` the `layout` object? Is it automatic? – Kingsley Dec 15 '21 at 23:49
46

You don't have to create a QVBoxLayout manually. Just select your central QWidget and press a make-layout button.

alt text

Spooky
  • 2,966
  • 8
  • 27
  • 41
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • 27
    Note that you must have at least one widget already on the central widget for the make-layout buttons to be enabled – Freedom_Ben May 23 '14 at 02:02
17

Add at least one widget on your MainWindow. Then select your window by clicking on it and click on the VerticalLayout Button at the top of QTCreator. You Vertical Layout is automatically added to the central widget and fills all the surface.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
  • 3
    Thank you. I couldn't figure it out because I didn't have any widgets on my MainWindow, so your answer was most helpful – Freedom_Ben May 23 '14 at 02:01
3

This is already answered, but I personally prefer to keep all control elements and layouts added manually to the form. I do not add controls in the class files, I merely hook up the signals/slots to hide/show widgets relevant to the logic in the class, within the class.

To manually add a layout to any widget you must first add at least one child widget/control. That wasn't totally clear to me and I was trying to add the layout first.

Rachael
  • 1,965
  • 4
  • 29
  • 55