Results 1 to 4 of 4

Thread: strange QGraphicsView sizing behavior

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default strange QGraphicsView sizing behavior

    I have created a main window with a QGraphicsView and worked it such that when the window is resized, the scene will remain in the window and scale itself to always fit within the QGraphicsView no matter what size it is. I used the following method to do this:

    Qt Code:
    1. WindowScene::WindowScene(QWidget *parent) : QMainWindow(parent) {
    2.  
    3.  
    4.  
    5. setupUi(this);
    6. database = db;
    7.  
    8. cview = new QGraphicsView;
    9. cview->installEventFilter(this);//set event filter
    10.  
    11. setCentralWidget(cview);
    12.  
    13. cscene = new QGraphicsScene;
    14. populateScene(); //put some junk in the scene for testing
    15.  
    16. cview->setScene(cscene);
    17.  
    18. createDockWindows();
    19. //add dock widget to the side
    20.  
    21.  
    22.  
    23. }
    24.  
    25. bool WindowScene::eventFilter(QObject *o, QEvent *e) {
    26. //process resize event of needed widget
    27. if (o == cview && e->type() == QEvent::Resize) {
    28. //rescale the scene to fit the view
    29. cscene->views().first()->fitInView(cview->sceneRect().toRect(),Qt::KeepAspectRatio);
    30. return true;
    31. }
    32. return QMainWindow::eventFilter(o, e);
    33. }
    To copy to clipboard, switch view to plain text mode 

    This seems to work for the most part, but there is a strange issue where the scene will not show up when I drag the window in certain ways:

    - If I expand the window to the side only, then the scene scaling works perfect
    - If I expand the window to up and down only then the scene scaling works perfect.
    - If I make the window smaller in the vertical and horizontal directions at the same time, the scene scales down.
    - If I make the window larger vertically and horizontally at the same time, then the scene moves, centered with the window, but the viewport does not expand and the scaling stops short of where it should in order to fill the viewable area.

    What might be causing this issue only when enlarging the window in both directions at same time? I have attached pictures so you can see what it is doing
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: strange QGraphicsView sizing behavior

    You might want to use qDebug() to trace what events are created during the resize.

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange QGraphicsView sizing behavior

    Using the following code as the event filter:
    Qt Code:
    1. bool DialogScene::eventFilter(QObject *o, QEvent *e) {
    2.  
    3. qDebug()<<"Object:"<< o <<"Event: "<<e->type();
    4.  
    5. //process resize event of needed widget
    6. if (o == calendarview && e->type() == QEvent::Resize) {
    7. qDebug()<<"Should fire fitInView...";
    8. //rescale the scene to fit the view
    9. calendarscene->views().first()->fitInView(calendarscene->sceneRect(),Qt::KeepAspectRatio);
    10.  
    11. return true;
    12. }
    13. return QMainWindow::eventFilter(o, e);
    14. }
    To copy to clipboard, switch view to plain text mode 

    I get the following results:
    When I drag the window larger in any of the ways that will make the scene resize, the event firing sequence is as follows:

    Qt Code:
    1. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    2. Should fire fitInView...
    3. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    4. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43 //fitInView happens here
    5. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    6. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    7. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    8. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    9. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    10. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    11. Should fire fitInView...
    12. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    13. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43 //fitInView happens here
    14. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    To copy to clipboard, switch view to plain text mode 

    QEvent::Resize -> QEvent::Paint -> QEvent::MetaCall -> QEvent::MetaCall ...

    The meta call happens over and over as I drag the boundries of the window. Commenting out the fitInView command causes the QEvent::MetaCall to not fire, so I assume QEvent::MetaCall is analogous to the fitInView command happening.

    When the glitch happens as seen in the picture, dragging the window boundaries produces the following events:

    Qt Code:
    1. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    2. Should fire fitInView...
    3. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    4. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    5. Should fire fitInView...
    6. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    7. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    8. Should fire fitInView...
    9. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    10. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    11. Should fire fitInView...
    12. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    To copy to clipboard, switch view to plain text mode 


    QEvent::Resize > QEvent::Paint > QEvent::Resize > QEvent::Paint... (over and over as I drag)

    however as soon as I drag the window small enough to where the glitched area hits the edge of the view, then I get the following event sequence:

    Qt Code:
    1. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    2. Should fire fitInView...
    3. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    4. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 76
    5. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43 //fitInView happens here
    6. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    7. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    8. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    9. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    10. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 14
    11. Should fire fitInView...
    12. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 12
    13. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43 //fitInView happens here
    14. Object: QGraphicsView(0xe42f10, name = "calendarview") Event: 43
    To copy to clipboard, switch view to plain text mode 

    QEvent::LayoutRequest > QEvent::Resize -> QEvent::Paint -> QEvent::MetaCall ...

    so it seems that if I drag the window too far, then the QEvent::MetaCall AKA fitInView stops working, but as soon as it gets smaller and the QEvent::LayoutRequest fires, then everything returns to its proper operation.

    It is also strange that the fitInView will not fire after its resized both vertical and horizontal at the same time, because the events show that the QEvent::Resize is triggering, and that the object that triggered it was "calendarview" so, there should be no reason why the if() statement does not evaluate to true and allow the fitInView command to work.
    Last edited by tpf80; 1st February 2009 at 03:21.

  4. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange QGraphicsView sizing behavior

    I also tested the fitinview command by connecting a button to a slot so I could manually trigger it. What I found, was that the same results happen for the manual triggering of it. Maybe fitinView is working, but the "viewport" is not the same size as the QGraphicsView.

    Is there a way to change the viewport size to match the QGraphicsView?

Similar Threads

  1. Replies: 0
    Last Post: 9th September 2008, 12:57
  2. Very strange behavior of QWidget in Qt Designer
    By THRESHE in forum Qt Programming
    Replies: 10
    Last Post: 13th August 2008, 16:19
  3. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  4. Strange behavior with Drag and Drop (not always drops)
    By Mona Shokrof in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2007, 18:00
  5. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49

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.