PDA

View Full Version : forcing QResizeEvent to come immediately



tpomorsk
14th September 2006, 12:46
Hi,
Is there a way to force QResizeEvent to come immediately?

According to the docs:


If the widget is visible when it is being resized, it receives a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.

The problem is I want to receive the event at once - without waiting for the widget to become visible.

I tried to invoke QCoreApplication::postEvent:


QResizeEvent resizeEvent(newSize, myWidget->size());
QCoreApplication::postEvent(myWidget, &resizeEvent);

but I receive a warning


QEvent: Warning: event of type 14 deleted while posted to MyFrame myWidget

and a segmentation fault just after.

Can I do something else?

Tomek

jpn
14th September 2006, 13:31
Try:


QResizeEvent* resizeEvent = new QResizeEvent(newSize, myWidget->size());
QCoreApplication::sendEvent(myWidget, resizeEvent); // QApplication takes ownership of the event


or even


myWidget->resize(newSize); // schedule a new resize event
QApplication::processEvents(); // process pending events

jacek
14th September 2006, 13:52
QCoreApplication::sendEvent(myWidget, resizeEvent); // QApplication takes ownership of the event
Unfortunately it doesn't behave like QCoreApplication::postEvent():

The event is not deleted when the event has been sent. The normal approach is to create the event on the stack, for example:
QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0);
QApplication::sendEvent(mainWindow, &event);
Because the event doesn't go through the event loop which is responsible for deleting the event.

jpn
14th September 2006, 14:19
Oops.. thanks jacek for pointing it out.

But anyway, for QCoreApplication::postEvent() you must allocate the event on the heap.

The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to modify or delete the event after it has been posted.

tpomorsk
14th September 2006, 16:37
Ok. Everything works fine.
JPN, as you said the event must be allocated on the heap.

Instead of this:


QResizeEvent resizeEvent(newSize, myWidget->size());
QCoreApplication::postEvent(myWidget, &resizeEvent);

I did this:


QResizeEvent* resizeEvent = new QResizeEvent(newSize, myWidget->size());
QCoreApplication::postEvent(myWidget, resizeEvent);


It seems that event must be put on the heap and not on the stack. Otherwise QApplication (or whatever is responsible for removing events) can't delete it.
But the warning I had:


QEvent: Warning: event of type 14 deleted while posted to MyFrame myWidget

suggests that the event was deleted before it was posted. I initialized it in a contructor so probably the contructor has finished and all the objects initialized in it were deleted.

Thanks very much for help
Tomek

jpn
14th September 2006, 16:39
But the warning I had:

suggests that the event was deleted before it was posted. I initialized it in a contructor so probably the contructor has finished and all the objects initialized in it were deleted.
Yes, indeed. The event object went out of scope and got destructed before it was posted.