Results 1 to 3 of 3

Thread: How to set definite Scene Rect in QGraphicsView.

  1. #1
    Join Date
    Oct 2008
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to set definite Scene Rect in QGraphicsView.

    Hi All,
    I am developing a application in which I am adding no of items into the scene. I know that by default view shows the item in the center. thats fine with my application. Now when I try to animate the items with setPosAt to move the items all the items gets togather at the center of the view overlapping eachother. I think this is the problem with SceneRect. Now I need to set the definitive scene rect . I have tried it to set using setSceneRect but it doesn't worked. Can anyone please help me out. Do I need to subclass QGraphicsScene class inorder to override setSceneRect.

    Thanks and Advance.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set definite Scene Rect in QGraphicsView.

    I dont think its prob with scene rect.
    You might be making some mistake while placing the items. Can we see what u are trying ??

  3. #3
    Join Date
    Oct 2008
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to set definite Scene Rect in QGraphicsView.

    Thanks for your reply..
    Here is the code.. Can you please tell me how can I scale the two rects at the center.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "animation.h"
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5. #include <QGraphicsItemAnimation>
    6. #include <QGraphicsEllipseItem>
    7. #include <QTimeLine>
    8. #include "windows.h"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13. //Animation w;
    14. //w.show();
    15.  
    16. QGraphicsRectItem *ball = new QGraphicsRectItem(150, 150, 100, 100);
    17. ball->setBrush(QBrush(QColor(255,0,0)));
    18. QGraphicsEllipseItem *ecl = new QGraphicsEllipseItem(150, 150, 20, 20);
    19. ecl->setBrush(QBrush(QColor(0,255,0)));
    20. QGraphicsRectItem *Rect = new QGraphicsRectItem(200, 175, 100, 50);
    21. Rect->setBrush(QBrush(QColor(0,0,255)));
    22. Rect->setZValue(-1);
    23.  
    24. QTimeLine *timer = new QTimeLine(15000);
    25. timer->setFrameRange(0, 100);
    26.  
    27. animation->setItem(ball);
    28. animation->setTimeLine(timer);
    29.  
    30. animation1->setItem(Rect);
    31. animation1->setTimeLine(timer);
    32.  
    33.  
    34. QGraphicsScene *scene = new QGraphicsScene();
    35. scene->addItem(ball);
    36. //scene->addItem(ecl);
    37. scene->addItem(Rect);
    38.  
    39.  
    40. QGraphicsView *view = new QGraphicsView(scene);
    41. view->setAlignment(Qt::AlignCenter);
    42. view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    43. view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    44. view->setGeometry(10,10,900,700);
    45. view->show();
    46.  
    47. //timer->start();
    48.  
    49. QRectF rc = ball->mapToScene(ball->boundingRect()).boundingRect();
    50. QRectF rc1 = ecl->mapToScene(ecl->boundingRect()).boundingRect();
    51. QRectF rc2 = Rect->mapToScene(Rect->boundingRect()).boundingRect();
    52.  
    53. for (int i = 0; i < 200; ++i)
    54. {
    55. //animation->setTranslationAt(i/200.0, rc.width()/2.0,rc.height()/2.0);
    56. animation->setScaleAt(i/200.0, 1 + i/200.0, 1 + i/200.0);
    57. //animation->setTranslationAt(i/200.0, -( rc.width()/2.0 + i/200.0), -rc.height()/2.0);
    58. animation->setPosAt(i/200.0, QPointF(150.0 - i/200.0, rc.top()));
    59. }
    60.  
    61. for (int i = 0; i < 200; ++i)
    62. {
    63. //animation1->setTranslationAt(i/200.0, -i/200.0,0);
    64. animation1->setScaleAt(i/200.0, 1 + i/200.0, 1 + i/200.0);
    65. //animation1->setTranslationAt(i/200.0, -rc2.width()/2.0, -rc2.height()/2.0);
    66. animation1->setPosAt(i/200.0, QPointF(150.0 -i/200.0, rc2.top()));
    67. }
    68.  
    69.  
    70. rc = ball->mapToScene(ball->boundingRect()).boundingRect();
    71. rc1 = ecl->mapToScene(ecl->boundingRect()).boundingRect();
    72. rc2 = Rect->mapToScene(Rect->boundingRect()).boundingRect();
    73.  
    74.  
    75. //scene->addItem(Rect);
    76.  
    77. //scene->setSceneRect(10, 10, 900, 700);
    78.  
    79. return a.exec();
    80. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th October 2008 at 16:37. Reason: missing [code] tags

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 21:50
  2. Setting the scene size in QGraphicsView
    By Valheru in forum Newbie
    Replies: 5
    Last Post: 11th November 2007, 16:03
  3. Scene focusing in QGraphicsView
    By mistertoony in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2007, 17:34
  4. How to use QGraphicsView and Scene right ...
    By Mike in forum Qt Programming
    Replies: 6
    Last Post: 22nd January 2007, 09:51

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.