How to update the contents of a QMainWindow to fit the window size

I have a QMainWIndow with multiple widgets, one of which is a QTableView.

The main WIndow file has dimensions.

How to resize the window automatically, font and that's it, i.e. when the window is resized, should its content automatically resize as well?

I would appreciate your help, thanks.

+3


source to share


1 answer


you have to put all your widgets in a layout, layouts will automatically resize all your widgets inside the QMainWindow, when the user made any changes to the size of the QMainWindow, you can add the layout both through the Qt Creator IDE and in Coding.

UPDATE:

if you add layouts to Qt Creator, the layouts are automatically encoded in the moc file and should not make any changes to their behavior through user coding.

but through coding in the class constructor:

QVBoxLayout *layout = new QVBoxLayout(parent);

layout->addWidget(widget1);   
layout->addWidget(widget2);   
layout->addWidget(widget3);   

this->setLayout(layout);  

      



but if you want to change the font of the QLabel it is done with the resizeEvent in the QMainWindow, so for any resize of the MainWindow, the resizeEvent is triggered, so you use this code

in mainwindow.h, you declare resizeEvent :

protected:
    void resizeEvent(QResizeEvent* event);

      

in mainwindow.cpp implement resizeEvent :

void MainWindow::resizeEvent(QResizeEvent *event)
{
    MainWindow::resizeEvent(event);
    if(this)
    {
        // QLabel process
    }
}

      

+2


source







All Articles