Results 1 to 4 of 4

Thread: QDialog resizeEvent not happening in time?

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default QDialog resizeEvent not happening in time?

    Hello,

    In trying to respond to a recent thread in this forum I wrote the following code:

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include <QtGui>
    3. #include <QDebug>
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. : QDialog(parent)
    7. {
    8. scene = new QGraphicsScene();
    9. view = new QGraphicsView(scene, this);
    10. }
    11.  
    12. void Dialog::resizeEvent(QResizeEvent *e)
    13. {
    14. qDebug() << "Dialog::resizeEvent()" << e->size();
    15. view->resize(e->size());
    16. scene->setSceneRect(view->viewport()->rect());
    17. scene->clear();
    18. scene->addRect(0, 0, scene->sceneRect().width()-1, scene->sceneRect().height()-1);
    19. QDialog::resizeEvent(e);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Adding a rectItem to the scene after it gets resized is simply to show myself that everything is sized as it should be. And this solution seems to work brilliantly if main.cpp looks like this:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "dialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8.  
    9. w.show();
    10. w.resize(500, 500);
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    With that code, I get the following debug output:

    Qt Code:
    1. Dialog::resizeEvent() QSize(131, 30)
    2. Dialog::resizeEvent() QSize(500, 500)
    To copy to clipboard, switch view to plain text mode 

    However, if the order of the method calls on w is reversed, I end up with only the following debug output:

    Qt Code:
    1. Dialog::resizeEvent() QSize(500, 500)
    To copy to clipboard, switch view to plain text mode 

    and get a small rectangle in the center of the dialog. Then, when I manually resize the window by even one pixel, the rectangle snaps into place and everything works from then on.

    Now, according to the documentation for QWidget::resize(),
    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.
    Based on what I see, I'm clearly not understanding what's going on here. That is, the event *is* being received but is not reflected in the output. Hopefully a good night's sleep will make all the pieces click but in the mean time I'm going crazy here. Can someone *please* try to explain why my little proof-rectangle apparently isn't being updated in time (or whatever this is)? Thanks a lot!
    Last edited by Urthas; 15th September 2010 at 05:47. Reason: added debug() output

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QDialog resizeEvent not happening in time?

    Use a layout instead of manually resizing the widgets inside your dialog.

    As for the resize event.

    Qt Code:
    1. w.show();
    2. // Is a size already set?
    3. // If not, use a default size.
    4. // A resize event happens with the default size 131, 30
    5.  
    6. w.resize(500, 500);
    7. // Resize the dialog
    8. // A resize event happens with the set size.
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. w.resize(500, 500);
    2. // Resize the dialog
    3. // A resize event happens with the set size.
    4.  
    5. w.show();
    6. // Is a size already set?
    7. // If yes, show with the set size.
    8. // A resize event is not needed, the dialog already has the correct size.
    To copy to clipboard, switch view to plain text mode 

    Edit:
    Your use of the scene and the rectangle item isn't really correct either.

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: QDialog resizeEvent not happening in time?

    The problem is that the dialog is being shown with the set size, and the view is also the set size, but (as further debug() statements have revealed*), the viewport's size is NOT reflective of the newly acquired view size! So I guess my question can be distilled down to the following:

    When a call to resize() precedes a call to show(), the view is properly resized, as requested. Why does the viewport not follow suit on this initial event, yet on manual resizing, both view and viewport are properly resized?

    *
    Qt Code:
    1. void Dialog::resizeEvent(QResizeEvent *e)
    2. {
    3. qDebug() << "Dialog::resizeEvent()" << e->size();
    4. view->resize(e->size());
    5. scene->setSceneRect(view->viewport()->rect());
    6.  
    7. qDebug() << "View: " << view->width() << "," << view->height();
    8. qDebug() << "Viewport: " << view->viewport()->width() << "," << view->viewport()->height();
    9. qDebug() << "Scene: " << scene->sceneRect().width() << "," << scene->sceneRect().height();
    10.  
    11. scene->clear();
    12. scene->addRect(0, 0, scene->sceneRect().width()-1, scene->sceneRect().height()-1);
    13. QDialog::resizeEvent(e);
    14. }
    To copy to clipboard, switch view to plain text mode 

    on show():
    View: 500 , 500
    Viewport: 98 , 28
    Scene: 98 , 28

    on subsequent manual resize, 1 pixel to the right:
    View: 501 , 500
    Viewport: 499 , 498
    Scene: 499 , 498
    Last edited by Urthas; 15th September 2010 at 06:31.

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: QDialog resizeEvent not happening in time?

    Your use of the scene and the rectangle item isn't really correct either.
    Ordinarily I would not add to the scene in the Dialog's resizeEvent(). It's there for purposes of quick-and-dirty visualization.

Similar Threads

  1. resizeEvent help pls
    By munna in forum Newbie
    Replies: 10
    Last Post: 9th July 2010, 08:38
  2. toolbutton action is not happening.
    By phillip_Qt in forum Qt Programming
    Replies: 13
    Last Post: 19th November 2009, 09:13
  3. hook resizeEvent
    By prashant in forum Qt Programming
    Replies: 6
    Last Post: 7th September 2009, 11:35
  4. What cannot be done in resizeEvent(..)?
    By nifei in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2008, 02:48
  5. Listbox Autoscroll is not happening?
    By mahe2310 in forum Qt Programming
    Replies: 4
    Last Post: 9th March 2006, 09:13

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.