PDA

View Full Version : Change layout at runtime glitch?



darktorres
15th August 2014, 01:49
I have a .ui mainwindow made with Design that have 3 images in a vertical layout. I made two others layout, horizontal and square, that use code to change the screen instead of Design. The problem, as seen in the image, is that when the layout is changed the views don't get resized correctly. They should have each 1/3 of the screen but one is big and the others small.

1055410555

If I keep clicking on the horizontal layout button on the left, the views get resized just a bit until they are the correct size.

Here's the code I use to change layouts:


void MainWindow::setHorizontalLayout() // layout horizontally
{
ui->canvas->layout()->removeWidget(ui->graphicsView0);
ui->canvas->layout()->removeWidget(ui->graphicsView1);
ui->canvas->layout()->removeWidget(ui->graphicsView2);
ui->canvas->layout()->removeWidget(ui->graphicsView3);
ui->canvas->layout()->removeWidget(ui->frame0Controls);
ui->canvas->layout()->removeWidget(ui->frame1Controls);
ui->canvas->layout()->removeWidget(ui->frame2Controls);
ui->canvas->layout()->removeWidget(ui->frame3Controls);
delete ui->canvas->layout();

QGridLayout *horizontalLayout = new QGridLayout;
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
ui->canvas->setLayout(horizontalLayout);
horizontalLayout->addWidget(ui->view0Label, 0, 0);
horizontalLayout->addWidget(ui->graphicsView0, 1, 0);
horizontalLayout->addWidget(ui->frame0Controls, 2, 0);
horizontalLayout->addWidget(ui->view1Label, 3, 0);
horizontalLayout->addWidget(ui->graphicsView1, 4, 0);
horizontalLayout->addWidget(ui->frame1Controls, 5, 0);
horizontalLayout->addWidget(ui->view2Label, 6, 0);
horizontalLayout->addWidget(ui->graphicsView2, 7, 0);
horizontalLayout->addWidget(ui->frame2Controls, 8, 0);
horizontalLayout->addWidget(ui->graphicsView3, 9, 0);
horizontalLayout->addWidget(ui->frame3Controls, 10, 0);
}

anda_skoa
15th August 2014, 09:22
That code is a bit of a contradiciton in naming.

The method is called setHorizontalLayout(), but it creates grid layout and it adds the widgets in different rows, i.e. layouting them vertically.

From your description and the screenshots I would guess that you want a QVBoxLayout, into which you add the control frames as-is and add the graphics views using the same stretch factor on each of the three calls.

Cheers,
_