26

Neither I could find a tutorial-like scheme for a resize event on QMainWindow, nor I did see any option for adding resize event in the drop-down menu at the Qt design window.

I am new to Qt. I'd like to write a slot function for a QMainWindow resize event. Is there such event? How can I do this?

Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • When I wrote this question, probably only Qt C++ existed. Since then PyQt was born, feel free to answer it for python as well. (although at the moment I don't have access to any of these) – Barney Szabolcs Mar 14 '22 at 18:30

3 Answers3

43

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).

RA.
  • 7,542
  • 1
  • 34
  • 35
  • I'd put the personal code before calling QMainWindow::resizeEvent(event) and not after because you can get information from this event before passing it to QMainWindow. Or even modify it before passing to QMainWindow. – Patapoom May 07 '20 at 13:32
6

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
}
Ingo Mi
  • 999
  • 12
  • 26
1

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