How to assign callback when the user resizes a QMainWindow?

There is a resize event. In order to perform custom handling of the event, you'll need to create your own resize event handler. In your case, you would need to create a class that derives from QMainWindow and reimplement the resizeEvent function. Your code would look something like this:

void MyMainWindow::resizeEvent(QResizeEvent* event)
{
   QMainWindow::resizeEvent(event);
   // Your code here.
}

The Qt Scribble example also has an example of overriding the resize event (though not on the main window).


In PyQt5 try the following:

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg

class MainWindow(qtw.QMainWindow):

    def __init__(self):
        super().__init__()
        self.resize(600, 600)
        # your code

    def resizeEvent(self, e: qtg.QResizeEvent):
        # your code

This works in Qt5 with me f.e. to resize the icon in a QTableWidget:

mainWindow.h
...
private:
void resizeEvent(QResizeEvent*);
...

mainWindow.cpp
...
void mainWindow::resizeEvent(QResizeEvent*)
{
    tableWidget->setIconSize(QSize(tableWidget->size()/7)); //7 or whatever number you need it to get the full icon size
}

Tags:

Qt

Qwidget