-1

I am using Creator to build main MainWindow and I populate it with all my widgets. I do not set any MainWindow lay out 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 Creator mode by pressing the left button.

I’ve tried hard all possible combinations reading many posts around. this solution: Qt: Can't set layout in QMainWindow doesn't work and it does not make much sense to me.

I've tried:

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

NO LUCK

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

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

NO LUCK

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

EDIT: To be more specific with NO LUCK I mean that the widgets are 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);
}

It is almost 2 days that I am googling and I can't find any way out

Thank you all for your help...

Rick64
  • 409
  • 1
  • 5
  • 7
  • 2
    `"NO LUCK"` isn't a very useful problem description. Aside from that, simply calling `centralWidget()->setLayout(...)` should set the central widget's layout. What it *won't* do is automatically add the central widget's existing child widgets to the new layout. A [mcve] would be useful. – G.M. Aug 31 '22 at 13:59
  • 1
    It is strange that this highly accepted answer does not work for you. Did you also have a look at the linked answer: https://stackoverflow.com/questions/1508939/qt-layout-on-qmainwindow/12454012#12454012 – RoQuOTriX Aug 31 '22 at 13:59
  • Did you get a complaint that the widget already had a layout? – drescherjm Aug 31 '22 at 14:05
  • @drescherjm No complain at all. – Rick64 Aug 31 '22 at 14:09
  • @RoQuOTriX I've tried that one also: // Set layout QGridLayout *layout = new QGridLayout; layout->addWidget(ui->MainWindowWidget); // Set layout in QWidget QWidget *window = new QWidget(); window->setLayout(layout); // Set QWidget as the central layout of the main window setCentralWidget(window); And nothing happened – Rick64 Aug 31 '22 at 14:11
  • @user2972027 please edit your answer. It is hard to understand the code in comments – RoQuOTriX Aug 31 '22 at 14:15
  • 1
    Are you really calling `ui->setupUi(this);` twice? – drescherjm Aug 31 '22 at 14:36
  • 1
    But where are widgets you add to layout? Central widget is a container that does NOT belong to layout, it's actually should own other widgets which have to be add to layout – Swift - Friday Pie Aug 31 '22 at 15:01

2 Answers2

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);
Ali-Ibrahim
  • 835
  • 1
  • 6
  • 16
  • 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 Aug 31 '22 at 14:32
  • There isn't extra widgets! `ui->label` and `ui->label_2` are the widgets you create using QTCreator. – Ali-Ibrahim Aug 31 '22 at 14:34
  • Also it's `MainWindowLayout` not `MainWindowWidget`. – Ali-Ibrahim Aug 31 '22 at 14:35
0

@C137

Finally I got it working doing the following: I places all my form widgets into 3 different widgets (containers QFrame in my case). Then I placed them into the Layout as suggested and it worked. This solution is a bit tricky

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

QT is handling this task in its own way a bit confusing from my point of view. While I was using Embarcadero the Layout were much much easier to manage.

I thought there could me a method to easily set the MainWindow Layout as in Creator mode which was much much easier and faster to handle.

So far it worked as expected but still confusing.

Thank you all for the support.

Rick64
  • 409
  • 1
  • 5
  • 7
  • 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. – Swift - Friday Pie Aug 31 '22 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 Aug 31 '22 at 15:33