Search code examples
qtqwidget

How to assign callback when the user resizes a QMainWindow?


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?


Solution

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