When is resizeEvent issued?
-
This is going back over thirty years, but I if I remember correctly before a window is opened a resize event is issued by the operating system (Window NT and OS2, not sure about Unix) I found it was not true for Qt . I want to update some transformation matrices when the window is resized including before when the window is displayed for the first time. I am using Qt6 and Qt Creator.
-
A resize event is posted whenever the size of a widget changes. Such changes can be programmatic, user interaction or propagated from your operating system. The latter ones vary depending on your OS type. It is not guaranteed that the construction of a widget triggers a resize event. In principle, a zero-to-X change would, a null-to-X change wouldn’t.
Best way to find it out, is to install an eventFilter on your object, listen to the resize event and qDebug the result. Note that due to asynchronous event handling, especially on Linux platforms, the order of resize events can change and some can even be triggered twice. If you want your code to be platform independent, you have to check this for each platform and interpret accordingly. It is guaranteed that events cause the indicated change on a widget. A specific order, however, is not guaranteed. You have to keep that in mind :-) -
Thanks for the quick answer. I resized the window with a qDebug in resizeEvent that did not show, then I realized that the window was promoted from QWidget to MyCanvas. Could QWidget not pass the the event to MyCanvas?
Qt 6.2.2 (x86_64-little_endian-llp64 shared (dynamic) release build; by MSVC 2019) on "windows"
OS: Windows 11 Version 2009 [winnt version 10.0.22000] -
@JonB
One of the OS dependent cases: whenshow()
is called on the widget, resize comes in tow. Same when showing is OS or user triggered on Windows. Linux might cause a different order on user interactions. -
@ofmrew
I would propose listening to the QWidget.
Is your event filter called at all? Do you install it from a class inheriting QObject and having the Q_Object macro at the right place? -
@Axel-Spoerl I just noted that the mainwindow resizes but not MyCanvas. I need to set it so that it also resizes when the mainwindow resizes. If that does not work then I will try the event filter. Thanks.
-
@ofmrew I use Qt Creator for all UI work. I added risizeEvent to MainWindow and I got the resizeEvent on initial opening. It looks as if MyCanvas never got notified. My next step is to put the canvas in a layout to cause a resize; however, in Designer that is easier said than done once the buttons and the promoted widget, canvas, have been added.
-
@ofmrew said in When is resizeEvent issued?:
@ofmrew It looks as if MyCanvas never got notified.
What exactly is MyCanvas? Your first mention of it is in your second post. Is it you own class inheriting from QWidget? As @JonB wrote, if it overrides correctly, it gets notified. Still there is nothing wrong about installing the event filter on the QWidget.
-
@Axel-Spoerl I want a white surface on which to draw, so I use Designer to place a Widget in the UI, then I promote that Widget to a new class MyCanvas, which does all the drawing. This allows me to use multiple Widgets, for different drawing purposes promoted to other classes. MyCanvas does inherit from QWidget.
I fixed the problem of MyCanvas resizing and I can say that MainWindow gets the resizeEvent before the MainWindow opens and all subsequent resizes. MyCanvas never gets a resizeEvent. I will install an event filter on QWidget. Since I have not done that before it I will need to do some research. I will let you know what I find.
-
@ofmrew said in When is resizeEvent issued?:
to place a Widget in the UI
And hopefully add it to a layout or as central widget - otherwise there will be no automatic resizing and you will never get a resizeEvent() due to this.
-
@Christian-Ehrlicher The steps are the following;
At bottom of MainWindow of Designer add button and spacers, select and add horizontal layout.
Above the button and on the MainWindow place a Widget, then right click on the MainWindow and add a vertical layout, which will be the layout for the central widget and the stop icon is removed from the central widget. What is shown in the Designer is not what you see when you execute the program. And it resizes when the MainWindow is resized, but never receives the resize message, only the MainWindow.Recall that my objective is to calculate transformation matrices for each resize, including the first wheb the window is first opened. I could use signals and slots to signal MyCanvas each time the MainWindow is resized. Not as clean as getting a resize event.
-
@ofmrew said in When is resizeEvent issued?:
MainWindow is resized, but never receives the resize message, only the MainWindow.
Then you're doing something wrong here.
Break it down to a simple QMainWindow without an ui-file, add your derived class as central widget and see if you get a resize event. Post the code here.