Qt World Summit: Register Today!

How to check for window size changes?

  • There are a number of tasks that I want to execute if the window size changes, is there a way to execute code when that happens? I've read some stuff on QResizeEvent but when functions get involved they usually involve passing a QResizeEvent to the function, which I do not know how to do. Is there a way with QResizeEvent or do I need to use something else in order to detect and execute code when the mainwindow size changes?

  • The code I came up with is:

    void MainWindow::resizeEvent(QResizeEvent *e) {
        int window_height = MainWindow::size().height();
        int window_width = MainWindow::size().width();
        ui->home_frame->resize(window_width, 110);
        ui->tab->resize(window_width, 110);

    which doesn't resize the frame at all... Any idea why it doesn't do that? I managed to resize a QTextEdit, so I know it works now, but I can't seem to resize the frame or the tab.


    ui->tabWidget->resize(window_width, 100);

    fixed it!

  • Lifetime Qt Champion


    Just in case, QResizeEvent gives you the new size of the widget.

    Out of curiosity, why are you doing resizing by hand rather than use a layout ?

Log in to reply