Results 1 to 8 of 8

Thread: setSceneRect not being called properly?

  1. #1
    Join Date
    Sep 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default setSceneRect not being called properly?

    Hi All,

    Using Qt4.3 I’m using a QGraphicsScene and QGraphicsView to show small portion of a large image (4600x6000 pix) and draw symbols upon it. Works fine (AFAIK). I then use the same scene and view to show the same size portion of a larger image (8000, 5000). The symbols I draw do not show up, and paint is only called once. But if I scroll my scene to the left, about half-way through, paint starts getting called again, but when I scroll back to my starting point, no symbols are drawn.

    It seems like I am not defining my view or scene properly, but I don’t see how to do it differently. I’m assuming as I scroll to the left and get to the half-way, my view is then showing the range of the first image, which was 4600 pixels wide.

    For simplicity, I’m leaving out details of the classes which are doing the painting, etc.

    Code snippet is below. Does anybody see what am I doing wrong?

    bjh


    Qt Code:
    1. // first image
    2.  
    3. view->setBackgroundBrush(QPixmao(image1));
    4. scene->setSceneRect(0, 0, 4600, 5500)
    5. view->resize(400, 300);
    6. // symbols drawn…
    7.  
    8. // second image
    9. scene->setSceneRect(0, 0, 8000, 5000);
    10. view->updateSceneRect(QRectF(0, 0, 8000, 5000));
    11. view->setbackgroundBrush(QPixmap(image2));
    12. // symbols not drawn…
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSceneRect not being called properly? RESOLVED!

    Hi All,

    Just found the root of my problem. I forgot to change my boundingRect() method when I changed the scene rectangle! So when I switched to a wider rectangle for the scene, only the old width of it was 'bounded'!

    Code now appears to work as expected; hope this post may save someone else from pulling out their hair! ;-)


    bjh

  3. #3
    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: setSceneRect not being called properly? RESOLVED!

    The bounding rect should be calculated in item's local coordinates. Global coordinates should be set using QGraphicsItem::setPos(). You probably didn't use it and that caused your problems.

  4. #4
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSceneRect not being called properly?

    I could not understand the relation between setSceneRect and bounding rectangle.

    I need to display a no. of rectangles each placed right to the previous rectangle. Now, as number of rectangles is high therefore i needed a scroll bar, so that i may scroll right to see rest of the rectangles.

    My GraphicsView size geometery is
    Qt Code:
    1. graphicsView->setGeometry(QRect(10, 8, 730, 540));
    To copy to clipboard, switch view to plain text mode 

    so i set my screen rectangle as:
    Qt Code:
    1. scene->setSceneRect(-360,-266,1000,532);
    To copy to clipboard, switch view to plain text mode 
    because 730-10 = 720 and 720/2 = 360. Similarly 540-8=532 and 532/2 = 266

    I set x2 = 1000 and not 720 because i wanted horizental scroll bars to appear.

    Eace time i add a rectangle i pass its size too. I made a tiny class for displaying my rectangles. When i execute my program the rectangles just blink and disappear immediately. The scroll bars do appear but i can't see any of my rectangles. Please help me out. I can't figure out the problem but i am trying.

    Qt Code:
    1. #include "rectMap.h"
    2. #include <QGraphicsScene>
    3. #include <QPainter>
    4. #include <QStyleOption>
    5. #include <math.h>
    6. static int xLocation = -360;
    7.  
    8. RectMap::RectMap(int Size)
    9. :color(qrand() % 256, qrand() % 256, qrand() % 256)
    10. {
    11. size = Size;
    12. }
    13.  
    14. QRectF RectMap::boundingRect() const
    15. {
    16. //...........................................................................................................
    17. I really do not have any idea wat my bounding rectangle should be
    18. //...........................................................................................................
    19. qreal adjust = 0.5;
    20. //return QRectF(-18 - adjust, -22 - adjust,36 + adjust, 60 + adjust);
    21. //return QRectF(-xLocation, -262, size, 523);
    22. return QRectF(-400,-262,1000,532);
    23. }
    24.  
    25. QPainterPath RectMap::shape() const
    26. {
    27. path.addRect(-100, -200, 20, 40);
    28. return path;
    29. }
    30.  
    31. void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    32. {
    33. painter->setBrush(color);
    34. painter->drawRect(xLocation,-262,size,523);
    35. updateLocation(size);
    36. }
    37.  
    38. void RectMap::updateLocation(int size)
    39. {
    40. xLocation = xLocation + size+ 3;
    41. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: setSceneRect not being called properly?

    This code is surely invalid. Changing location as a result of a repaint is probably not what you want.
    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.


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

    qtzcute (13th July 2009)

  7. #6
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSceneRect not being called properly?

    Sorry but i am very new in Qt's world and could not understand what you said. Can u kindly tell me where am i changing the location?

    If you are talking about updating the 'xLocation' integer variable in paint function then well, i am doing that because i want each new rectangle to be drawn at new place.

    If not, then please guide as to what i need to do.

    Thanks a lot wysota

  8. #7
    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: setSceneRect not being called properly?

    Quote Originally Posted by qtzcute View Post
    Sorry but i am very new in Qt's world and could not understand what you said. Can u kindly tell me where am i changing the location?
    In the paint() routine.

    If you are talking about updating the 'xLocation' integer variable in paint function then well, i am doing that because i want each new rectangle to be drawn at new place.
    That's surely not the right way to do this. paint() will be called for each of the rectangles more than once.

    In my opinion you should stick to QGraphicsRectItem or its subclass.
    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.


  9. The following user says thank you to wysota for this useful post:

    qtzcute (13th July 2009)

  10. #8
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setSceneRect not being called properly?

    Many thanks wysota...problem solved.

    You were right i should not have change the position in paint function...Now my rectangles are appearing quite finely.

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.