Home > Net >  Change QMainWindow layout programmatically in c
Change QMainWindow layout programmatically in c

Time:09-01

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...

CodePudding user response:

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

CodePudding user response:

@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.

  • Related