-1

I am using Qt Creator to build a main MainWindow and populate it with all my widgets. I do not set any MainWindow layout in this stage (like "Lay out in a Grid" or "Lay out Horizontally".

When I launch the application I want to dynamically change the MainWindow layout of widgets in it to "Lay out in a Grid" like in Qt Designer by pressing the left button.

I’ve tried hard all possible combinations reading many posts around. The solution in Qt: Can't set layout in QMainWindow doesn't work.

I've tried:

QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
centralWidget()->setLayout(MainWindowLayout);

I've tried to put all my widgets inside a big widget at design time named MainWindowWidget and then setting it as a centralWidget:

QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
setCentralWidget(ui->MainWindowWidget);
centralWidget()->setLayout(MainWindowLayout);

both attempts resulted in the widgets not placed as in a grid as expected.

Here is a code snipped that you can try on an empty application

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
        /* 
    Place some widgets at design time with Creator (at least 2) in the MainWindow form 
    in a misplaced order and do not apply any "Lay out xxx" right button on QT Creator
    */

    ui->setupUi(this);

    /* HERE I WANT THE MainWindow or either an Object to take a specific Layout */
    QGridLayout * MainWindowLayout = new QGridLayout;
    ui->setupUi(this);
    centralWidget()->setLayout(MainWindowLayout);
}

Is there any way to change the MainWindow widget's layout like "Lay ouy in a Grid" at design time when using Qt Designer?

0

2 Answers 2

0

You are creating the layout But you are not adding widgets to it. This should fix your issue:

ui->setupUi(this);
QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->label, 0, 0);
MainWindowLayout->addWidget(ui->label_2, 0, 1);
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);
3
  • My idea is not to create extra widgets as they are all present at design time. I tried your code adding the main object named MainWindowWidget and it shows up abosultely minimized in a part of the main window. My question is simple can't you set a "Lay out in a Grid" option programmatically as you would do while you are using QTCreator??!? Quite simple without finding workarounds like extra Widgets etc...
    – Rick64
    Commented Aug 31, 2022 at 14:32
  • There isn't extra widgets! ui->label and ui->label_2 are the widgets you create using QTCreator. Commented Aug 31, 2022 at 14:34
  • Also it's MainWindowLayout not MainWindowWidget. Commented Aug 31, 2022 at 14:35
0

Finally I got it working doing the following:

I placed all my form widgets into 3 different widgets (QFrames as containers in my case). Then I placed them into the layout as suggested and it worked.

QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->MainFrame, 0, 0); // MainFrame --> My new object containing other widgets
MainWindowLayout->addWidget(ui->DebugButtonsFrame, 0, 1);  // DebugButtonsFrame as above
MainWindowLayout->addWidget(ui->DebugMemoFrame, 1, 0); // DebugMemoFrame as above
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);
2
  • It's mostly because MainWindow is different from just a widget and have etra functionality attached. In some cases it's even better to not use a main window at all - Qt allows that. And dynamic change of whole layout is not what is usually done because on some platforms it's rather costy operation. Commented Aug 31, 2022 at 15:17
  • 1
    Isn't this essentially the same as @c137 answered outside of using frames instead of individual widgets? One thing though if you have frames that contain your other widgets you probably have layouts in those frames.
    – drescherjm
    Commented Aug 31, 2022 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.