Results 1 to 6 of 6

Thread: forcing QResizeEvent to come immediately

  1. #1
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default forcing QResizeEvent to come immediately

    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:ostEvent:
    Qt Code:
    1. QResizeEvent resizeEvent(newSize, myWidget->size());
    2. QCoreApplication::postEvent(myWidget, &resizeEvent);
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: forcing QResizeEvent to come immediately

    Try:
    Qt Code:
    1. QResizeEvent* resizeEvent = new QResizeEvent(newSize, myWidget->size());
    2. QCoreApplication::sendEvent(myWidget, resizeEvent); // QApplication takes ownership of the event
    To copy to clipboard, switch view to plain text mode 

    or even
    Qt Code:
    1. myWidget->resize(newSize); // schedule a new resize event
    2. QApplication::processEvents(); // process pending events
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: forcing QResizeEvent to come immediately

    Quote Originally Posted by jpn
    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.

  4. The following user says thank you to jacek for this useful post:

    tpomorsk (14th September 2006)

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: forcing QResizeEvent to come immediately

    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.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    tpomorsk (14th September 2006)

  7. #5
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: forcing QResizeEvent to come immediately [SOLVED]

    Ok. Everything works fine.
    JPN, as you said the event must be allocated on the heap.

    Instead of this:
    Qt Code:
    1. QResizeEvent resizeEvent(newSize, myWidget->size());
    2. QCoreApplication::postEvent(myWidget, &resizeEvent);
    To copy to clipboard, switch view to plain text mode 
    I did this:
    Qt Code:
    1. QResizeEvent* resizeEvent = new QResizeEvent(newSize, myWidget->size());
    2. QCoreApplication::postEvent(myWidget, resizeEvent);
    To copy to clipboard, switch view to plain text mode 

    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

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: forcing QResizeEvent to come immediately [SOLVED]

    Quote Originally Posted by tpomorsk
    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.
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.