3
votes

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

The Main WIndow is resizable.

How do I resize the contents of the window automatically, the font and everything, i.e. when the window is resized, its contents should also get resized automatically?

I'd appreciate the help, thanks.

1
Did you place your widgets into a layout?hank
resizing the fonts need to be done manually. But this is very tricky, as resizing fonts may trigger a new sizing of your widget, and of your main window.UmNyobe
@Hank - I have a Vertical box inside the window. In that window I've 3 widgets, 1 uses a Grid Layout, and has QLablels, the other a TableView using a TableLayout. If the table is resized, the contents within these should also be resized...Kindly advise.user1173240
I don't think you can do this with standard Qt mechanics as what you describe differs from what is considered the default behaviour (resize any application - the font size usually stays the same). You could probably override QWidget::render() or similar and use scale on the supplied QPainter. I'm not sure if this cascades through the subwidgets though, and even if so, border lines etc would be scaled as well.Tim Meyer
@TimMeyer I see. This would be quite a complex thing keeping track of properties of each subwidget. I'll give your suggestion a go.Thanks for your help.user1173240

1 Answers

2
votes

you should put all of your widgets into a layout, layout(s) automatically resize all of your widget(s) inside QMainWindow when user made any change in QMainWindow's size, you can add layout both via Qt Creator IDE and Coding.

UPDATE:

if you add layouts in Qt Creator, layouts automatically coded in moc file and no need to made any changes in their behaviour via coding by user.

but via coding in class constructor:

QVBoxLayout *layout = new QVBoxLayout(parent);

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

this->setLayout(layout);  

but if want to change QLabel's font, this is done by resizeEvent in QMainWindow, so for any change in size of MainWindow, resizeEvent 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
    }
}