Results 1 to 12 of 12

Thread: QGraphicsScene size

  1. #1
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsScene size

    Hi

    I am trying to create a GraphicsView where I can draw using the entire window. To do this I create a GraphicsScene with the view's rect as constructor. The result is that I get scrollbars (although it shouldnt be needed as the scene should be the same size as the view) that are pre-scrolled (why does it scroll by itself?) a few pixels in both directions. What am I doing wrong here?

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsView>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsSvgItem>
    5.  
    6. int main(int argc, char* argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. view->resize(640, 480);
    11.  
    12.  
    13. QGraphicsScene *scene = new QGraphicsScene(view->rect());
    14. scene->addItem(new QGraphicsSvgItem("ball.svg"));
    15. view->setScene(scene);
    16.  
    17.  
    18. view->show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    QRect is a little weird.
    The size is actually with 1 pxiel smaller in each direction( 639x479).
    So you have to adjust it a bit.

    Also, I used setFixedxSize since if you resize it, the scroll bars appear again.

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. view->setFixedSize(640, 480);
    6.  
    7.  
    8. QRect r = view->rect();
    9. r.adjust(1,1,-1,-1);
    10. QGraphicsScene *scene = new QGraphicsScene(r);
    11. //scene->addItem(new QGraphicsSvgItem("ball.svg"));
    12. view->setScene(scene);
    13. view->show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Regards

  3. #3
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Quote Originally Posted by marcel View Post
    QRect is a little weird.
    The size is actually with 1 pxiel smaller in each direction( 639x479).
    What is the reason for this behaviour? Seems a bit odd to me...

    The adjustment didnt fix it for me either

    I also get the same behaviour if I use view->width() / height() instead of the rect. And I still think its weird that it decides to autoscroll a couple of pixels.
    Last edited by invictus; 10th August 2007 at 18:33.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    The count starts at 0.
    Also, the view also has some borders that you must consider, since they are part of the bounding box.
    I guess you have to query the current style for the border
    width and subtract it from the rect size, in order to get the same results for different styles.

    Regards

  5. #5
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Quote Originally Posted by marcel View Post
    The count starts at 0.
    Also, the view also has some borders that you must consider, since they are part of the bounding box.
    I guess you have to query the current style for the border
    width and subtract it from the rect size, in order to get the same results for different styles.

    Regards
    Btw, will the childrenRect property perhaps give the same as rect minus border/margin/padding?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Quote Originally Posted by invictus View Post
    Btw, will the childrenRect property perhaps give the same as rect minus border/margin/padding?
    Well, I was wrong:
    size() and the other functions in this family do not include the widget's borders.
    So, you're ok adjusting the rect.

  7. #7
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Quote Originally Posted by marcel View Post
    Well, I was wrong:
    size() and the other functions in this family do not include the widget's borders.
    So, you're ok adjusting the rect.
    I wish I were...but I really can not get this to work. Perhaps there is something wrong with my computer system as I am running gnome to test this? No matter what I do and how adjusted the rect is, the item will always be partially outside the view... but if I can scroll I can scroll it "back" into the view. This is really weird I think

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QGraphicsScene size

    Quote Originally Posted by invictus View Post
    What is the reason for this behaviour?
    Backward compatibility with earlier Qt versions.

  9. #9
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    I was wondering...is there any other widget than QGraphicsView I can use for 2D graphics? And can I render SVG/Vector graphics on this (if such a widget exists)? I think perhaps GraphicsView is too complicated for my use when all I want is to do some custom drawings on a given portion of the application window.

    My book doesnt cover graphicsview either, while it has a short chapter on 2D using QPainter...

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    I was wondering...is there any other widget than QGraphicsView I can use for 2D graphics? And can I render SVG/Vector graphics on this (if such a widget exists)? I think perhaps GraphicsView is too complicated for my use when all I want is to do some custom drawings on a given portion of the application window.
    You can render a svg on virtually any paint device by using QSvgRenderer.
    This includes widgets and pixmaps. You could use, for example a QWidget, or a QLabel if you wish.


    My book doesnt cover graphicsview either, while it has a short chapter on 2D using QPainter...
    Because the Graphics View Framework is relatively new.
    Try this: http://doc.trolltech.com/4.3/graphicsview.html\

    Regards

  11. #11
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    If I can do that, why would I use graphicsview to begin with? Sorry if my questions are too stupid to be answered

    To be honest, what I am trying to do is to create a small puzzle game using SVG images for scalability.

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    If I can do that, why would I use graphicsview to begin with?
    The disadvantage, as I see it, when using widgets is that you will have to provide your own item interaction implementation( move puzzle pieces around, etc ).

    With a QGraphicsView/scene these things are built-in.
    Basically, all you have to do is concern on the drawing stuff.

    Regards

Similar Threads

  1. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  2. Font size calculation when painting in a QImage
    By Ishark in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 22:22
  3. How can I make size of QGraphicsScene smaller?
    By troorl_ua in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2007, 07:56
  4. Replies: 1
    Last Post: 24th October 2006, 16:40
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.