Results 1 to 20 of 20

Thread: How to move/repaint object in a graphicsscene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: How to move/repaint object in a graphicsscene

    Quote Originally Posted by wysota View Post
    The boundingRect defines the area of your item. If boundingRect() returns (-48,64, 98,24) then your item "starts" at (-48,64) and "lasts" until (50,88).
    How can you calculate the end point?

    Quote Originally Posted by wysota View Post
    The player can only resize the view, not the scene.
    But when the view is resized the scene is also resized.

    Quote Originally Posted by wysota View Post
    Please provide a minimal compilable example reproducing this behaviour.
    It is a multifile project so I attached it along with this reply

    Qt Code:
    1. QGraphicsRectItem *item = scene.addItem(QRectF(-100,-50, 200, 100), QPen(Qt::red)); // adds an item to the scene
    2. qDebug() << item->boundingRect() << item->pos() << item->sceneBoundingRect();
    3. item->setPos(50,50);
    4. qDebug() << item->boundingRect() << item->pos() << item->sceneBoundingRect();
    To copy to clipboard, switch view to plain text mode 

    The outputs are identical.
    Attached Files Attached Files

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

    Default Re: How to move/repaint object in a graphicsscene

    Quote Originally Posted by qtgames View Post
    How can you calculate the end point?
    Please read the docs for QRectF.

    But when the view is resized the scene is also resized.
    No, it's not.

    The outputs are identical.
    Did you try it in your application or a verbatim one? Create an application that consists of only a pure scene, pure view and the above calls.
    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. The following user says thank you to wysota for this useful post:

    qtgames (18th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: How to move/repaint object in a graphicsscene

    Quote Originally Posted by wysota View Post
    No, it's not.
    When I click to the bottom right corner the event->x() is different than when I resize the window and click bottom right corner and read event->x(). And I pull the scroll bars to the end. How is that possible?

    Quote Originally Posted by wysota View Post
    Did you try it in your application or a verbatim one? Create an application that consists of only a pure scene, pure view and the above calls.
    The pure one doesn't compile. addItem doesn't have such definition.

  5. #4
    Join Date
    Oct 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: How to move/repaint object in a graphicsscene

    Ok, some progress. The click, vehicle->pos() and the sceneBoundingRect() are at the same location. However, when the items are added into the scene, their vehicle->pos() is at (0,0), but they are 80,80 point below the top left corner.

    More progress. The scene was not initialized at 0,0 in the BattleField and that's why there was an offset. Now the only problem is that repaint doesn't work. I have to manually refresh.
    And setViewportUpdateMode(FullViewportUpdate) solved this. Thanks.
    Last edited by qtgames; 18th October 2009 at 09:06.

  6. #5
    Join Date
    Oct 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: How to move/repaint object in a graphicsscene

    Actually there is still a problem with the scroll bars. The movement works ok, when we at the top left corner of the scene. However, in bottom right corner the event->x() gives coordinates relative to the top left corner of the view, even if there is more scene behind the scroll bars. How can I get absolute coordinates in the scene?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to move/repaint object in a graphicsscene

    Quote Originally Posted by qtgames View Post
    When I click to the bottom right corner the event->x() is different than when I resize the window and click bottom right corner and read event->x(). And I pull the scroll bars to the end. How is that possible?
    Add this code to your program:

    Qt Code:
    1. QGraphicsRectItem *item = scene.addRect(scene.sceneRect(), QPen(Qt::red), Qt::yellow));
    2. item->setZValue(-1);
    To copy to clipboard, switch view to plain text mode 
    item->setOpacity(0.5);
    This will add an item to the scene which is the size of the scene itself. Then start manipulating the window size and see how the scene is moved in the view (you have to trust me its size doesn't change).

    The pure one doesn't compile. addItem doesn't have such definition.
    Man...

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QGraphicsScene scene(-400,-400,800,800);
    6. view.setScene(&scene);
    7. QGraphicsRectItem *item = scene.addRect(QRectF(-40, -20, 80, 40), QPen(Qt::red));
    8. qDebug() << item->pos() << item->boundingRect() << item->sceneBoundingRect();
    9. item->setPos(QPointF(100,80));
    10. qDebug() << item->pos() << item->boundingRect() << item->sceneBoundingRect();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Now the only problem is that repaint doesn't work. I have to manually refresh.
    And setViewportUpdateMode(FullViewportUpdate) solved this. Thanks.
    Bad idea. Find out why it doesn't refresh or else you'll have more problems later.

    You can add this to your item's paint() routine:

    Qt Code:
    1. painter->drawRect(boundingRect());
    To copy to clipboard, switch view to plain text mode 

    This will show you if you're drawing in the proper rectangle.
    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.


Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 20:39
  3. Replies: 4
    Last Post: 19th February 2009, 11:10
  4. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  5. A form returning an object.
    By cbarmpar in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 05:21

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
  •  
Qt is a trademark of The Qt Company.