Question

I have an OpenGL widget (myWidget) inside a QVBoxLayout (verticalLayout_2), in a Qt form class (MyForm).

I want to resize the OpenGL widget when the form has been resized.

What should I write inside the changeEvent method, to resize the QVBoxLayout and the OpenGL widget to the new size?

MyForm::MyForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyForm)
{
    ui->setupUi(this);
    myWidget = new GL_Widget;
    ui->verticalLayout_2->addWidget( myWidget );
    adjustSize();
}

void MyForm::changeEvent(QEvent *e)
{
    // What should I write here?
}
Was it helpful?

Solution

You shouldn't have to write any code to automatically resize your child widget. (Also, changeEvent() is not the correct event handler for this purpose anyway.)

Your problem is likely with the layout that you've created with Qt Designer. You probably dragged a vertical layout from the sidebar into your form widget, which is actually doesn't create a top-level layout. It's very easy to make this mistake.

Instead, do one of the following to create top-level layouts in Qt Designer:

  • Right-click on your top-level (form) widget in the Object Inspector, and choose one of the layouts from the "Lay Out" menu.

  • Select your top-level widget and click on one of the layout icons in the toolbar:

    qt designe r toolbar

If you set up your layout(s) correctly, you don't need to add any code. Your layout will update automatically if you preview your form in Qt Designer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top