Results 1 to 6 of 6

Thread: Understanding GraphicsView Viewport Size

  1. #1
    Join Date
    Mar 2015
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Understanding GraphicsView Viewport Size

    Hi,

    I know that I found a similar topic somewhere this for ex. but it does not help me.
    Anyway, I really can't figure out why the graphicsview always returns QSize(98,28) as it's size, geometry and viewport size upon construction.

    It's a problem because I intend to draw something on the scene based on the actual viewport coordinates.
    This of course is not possible since I get the wrong viewport size results.

    Here is a small snippet of what I did and would like to do:

    Qt Code:
    1. //inside a QMainWindow constuctor:
    2. Mainwindow::Mainwindow(QWidget *parent)
    3. : QMainWindow(parent)
    4.  
    5. {
    6. ...
    7. QGraphicsScene *scene = new QGraphicsScene(this);
    8. QGraphicsView *view = new QGraphicsView(this);
    9. view->setScene(scene);
    10. view->show();
    11. drawSomething();
    12. qDebug() << view->viewport()->size();
    13. qDebug() << view->geometry();
    14. ...
    15. }
    16.  
    17. ...
    18. void Mainwindow::drawSomething()
    19. {
    20. //draw 5 lines equally spaced aroud the width of the viewport
    21. //does not work because I get the 98 pixels as the width of the viewport width
    22. qreal intervall = view->viewport()->width() / 5;
    23. for(int i = 0; i < 5; ++i)
    24. {
    25. scene->addLine(intervall*i, 0, intervall*i, 1);
    26. }
    27. }
    28.  
    29. // returns
    30. QSize(98, 28)
    31. QRect(0,0 100x30)
    To copy to clipboard, switch view to plain text mode 

    These values are not the actual values of the Graphicsview of course. Now the problem is that I need to draw lines for ex.
    based on the actual viewport size.

    Many thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Understanding GraphicsView Viewport Size

    Quote Originally Posted by vgrunert View Post
    Anyway, I really can't figure out why the graphicsview always returns QSize(98,28) as it's size, geometry and viewport size upon construction.
    Widgets have bogus sizes until they are shown for the first time (simply calling show() does not make them visible).

    It's a problem because I intend to draw something on the scene based on the actual viewport coordinates.
    That rarely makes sense. Are you sure you don't want to draw it as the view's background?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2015
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Understanding GraphicsView Viewport Size

    Thank you for the quick reply. No, in fact I am not sure.
    But I want the lines to be scaled along with changes in resize events.
    Hence I thought that the right approach would be to calculate the lines expressed in their viewport coordinates.


    Btw

    Qt Code:
    1. ...
    2. view->setVisible(true);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    does not work either.

    So how do I draw the view at it's construction?
    Last edited by vgrunert; 14th April 2015 at 17:14.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Understanding GraphicsView Viewport Size

    Quote Originally Posted by vgrunert View Post
    Thank you for the quick reply. No, in fact I am not sure.
    But I want the lines to be scaled along with changes in resize events.
    Hence I thought that the right approach would be to calculate the lines expressed in their viewport coordinates.
    If you want something to be in viewport coordinates then it doesn't seem to belong in the scene. In my opinion you should implement it in QGraphicsView::drawBackground() reimplementation.


    Btw

    Qt Code:
    1. ...
    2. view->setVisible(true);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    does not work either.
    because that's not the point. show() only says "I want to be visible". The actual magic happens when the application processes that event.

    So how do I draw the view at its construction?
    You don't.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2015
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Understanding GraphicsView Viewport Size

    Thank you again. Yes that looks about right. I'll give it a try and post my results once done.

    bw

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Understanding GraphicsView Viewport Size

    If you want to get the actual size of a widget, you can override the showEvent(). By the time it gets there, the initial size and layout has been calculated and dimensions are correct. If you want to respond to changes in size, you also need to handle resizeEvent(). But beware, resizeEvent() can be called before showEvent(), so you have to wait until showEvent() to ensure that resizeEvent() contains correct widget dimensions.

Similar Threads

  1. Replies: 10
    Last Post: 8th April 2015, 18:38
  2. Understanding QSizePolicy
    By JaV0 in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2012, 19:07
  3. How to enlarge sceneRect() to the viewport size
    By iw2nhl in forum Qt Programming
    Replies: 3
    Last Post: 27th August 2007, 16:39
  4. changing QLabel font size(label created on the graphicsView)
    By maverick_pol in forum Qt Programming
    Replies: 11
    Last Post: 17th August 2007, 08:36
  5. ViewPort Properties
    By Kapil in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 11:57

Tags for this Thread

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.