Results 1 to 20 of 20

Thread: How to move/repaint object in a graphicsscene

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

    Default How to move/repaint object in a graphicsscene

    Hi,

    I'm trying to make a little tank game with qt and I'm stuck with moving items in a QGraphicsScene. What I would want to happen is that the unit would move to the location in the scene where there presses with a mouse, but I can't get it moving anywhere, which was what I have been trying to do. What am I missing?

    Qt Code:
    1. #include "armoredvehicle.h"
    2. #include <QGraphicsItem>
    3. #include <QtGui>
    4.  
    5. ArmoredVehicle::ArmoredVehicle(QPixmap turret, QPixmap hull)
    6. {
    7. unitTurret = new QGraphicsPixmapItem(turret);
    8. unitHull = new QGraphicsPixmapItem(hull);
    9. dx=0;
    10. dy=0;
    11. }
    12.  
    13. QRectF ArmoredVehicle::boundingRect() const
    14. {
    15. return QRectF(-75.5, -75.5, 74, 74);
    16. }
    17.  
    18. void ArmoredVehicle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    19. {
    20. Q_UNUSED(option);
    21. Q_UNUSED(widget);
    22. painter->setPen(Qt::NoPen);
    23. painter->setBrush(Qt::blue);
    24. painter->drawPixmap(this->x()+dx,this->y()+dy,QPixmap(40,20));
    25.  
    26. painter->setPen(Qt::NoPen);
    27. painter->setBrush(Qt::darkGray);
    28. painter->drawPixmap(this->x()+dx,this->y()+dy,QPixmap(3,30));
    29. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "battlefield.h"
    2. #include <QGraphicsView>
    3. #include <QGraphicsItem>
    4. #include "armoredvehicle.h"
    5. #include <QPixmap>
    6. #include <QMouseEvent>
    7.  
    8. BattleField::BattleField()
    9. {
    10. QGraphicsScene *scene = new QGraphicsScene(this);
    11. vehicle = new ArmoredVehicle(QPixmap(40,20),QPixmap(3,30));
    12.  
    13. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    14. scene->setSceneRect(-400, -400, 600, 600);
    15. scene->addItem(vehicle);
    16.  
    17. setScene(scene);
    18. }
    19.  
    20. void BattleField::mousePressEvent(QMouseEvent *event)
    21. {
    22. vehicle->unitHull->moveBy(vehicle->unitHull->x() - event->x(),vehicle->unitHull->y() - event->y());
    23. vehicle->unitTurret->moveBy(vehicle->unitTurret->x() - event->x(),vehicle->unitTurret->y() - event->y());
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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, I got it somehow redrawn, but I still can't understand the coordinate system. item->x() returns 0 and item->scenepos().x() returns zero. How can I get the position of the item in the scene in coordinates relative to the scene?

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

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

    QGraphicsItem::pos() returns a position of the origin of the item in its parent's (or scene if it has no parent) coordinates. I'd say that your bounding rect looks odd and that's why you are confused.
    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.


  4. #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

    Quote Originally Posted by wysota View Post
    QGraphicsItem::pos() returns a position of the origin of the item in its parent's (or scene if it has no parent) coordinates. I'd say that your bounding rect looks odd and that's why you are confused.
    I changed it to this:

    Qt Code:
    1. return QRectF(unitHull->x(), unitHull->y(), 40, 40);
    To copy to clipboard, switch view to plain text mode 

    That item->pos().x() gives also zero.

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

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

    The bounding rect specifies the local coordinate system of the item (not of its parent). You should probably use QRectF(-20,-20,40,40) and then use QGraphicsItem::setPos() or QGraphicsItem::move() to position the item on the scene.
    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. #6
    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 bounding rect specifies the local coordinate system of the item (not of its parent). You should probably use QRectF(-20,-20,40,40) and then use QGraphicsItem::setPos() or QGraphicsItem::move() to position the item on the scene.
    I still can't get the position and now the item keeps getting smaller after every click and finally disappears.

    Qt Code:
    1. ArmoredVehicle::ArmoredVehicle(QPixmap turret, QPixmap hull)
    2. {
    3. unitTurret = new QGraphicsPixmapItem(turret);
    4. unitHull = new QGraphicsPixmapItem(hull);
    5. int bRectLeft = -20;
    6. int bRectTop = -20;
    7. int bRectW = 40;
    8. int bRectH = 40;
    9. }
    10.  
    11. void ArmoredVehicle::moveUnit(QPointF newPoint)
    12. {
    13. this->setPos(newPoint);
    14. update();
    15. }
    16.  
    17. QRectF ArmoredVehicle::boundingRect() const
    18. {
    19. return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
    20. }
    21.  
    22. void ArmoredVehicle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    23. {
    24. Q_UNUSED(option);
    25. Q_UNUSED(widget);
    26. painter->setPen(Qt::NoPen);
    27. painter->setBrush(Qt::blue);
    28. painter->drawPixmap(this->pos().x(),this->pos().y(),QPixmap(3,30));
    29.  
    30. painter->setPen(Qt::NoPen);
    31. painter->setBrush(Qt::green);
    32. painter->drawPixmap(this->pos().x(),this->pos().y(),QPixmap(40,20));
    33.  
    34. qDebug("This pos: %d", this->pos().x());
    35. }
    36.  
    37. QRectF ArmoredVehicle::getBoundingRect()
    38. {
    39. return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
    40. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. BattleField::BattleField()
    2. {
    3. QGraphicsScene *scene = new QGraphicsScene(this);
    4.  
    5. vehicle = new ArmoredVehicle(QPixmap(40,20),QPixmap(3,30));
    6.  
    7. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    8. scene->setSceneRect(-100, -100, 1600, 1600);
    9.  
    10. scene->addItem(vehicle);
    11. setScene(scene);
    12. }
    13.  
    14. void BattleField::mousePressEvent(QMouseEvent *event)
    15. {
    16. qDebug("VehX before %d", vehicle->pos().x());
    17. vehicle->moveUnit(QPointF(event->x(),event->y()));
    18. qDebug("VehX after %d", vehicle->pos().x());
    19. qDebug("Widget posX %d", vehicle->pos().x());
    20. qDebug("X relative to widget(scene?): %d", event->x());
    21. qDebug("Y relative to widget(scene?): %d", event->y());
    22. qDebug("VehX %d", vehicle->unitHull->x());
    23. }
    To copy to clipboard, switch view to plain text mode 

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

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

    That's because you still don't understand the concept of a local coordinate system. Your item only knows of its own coordinate system and not of its parents. Consider a real world example - we're living on Earth which is part of the Solar system. When you are calculating distances or pointing places, you do it in Earth's coordinate system without taking into consideration that Earth is constantly revolving around the Sun (which is revolving around the centre of the Milky Way, etc.). Therefore in your paint() routine you should only use your own coordinate system and not your parent's. pos() is the position of yourself in your parent's coordinate system. In other words you can only draw within the coordinates returned by your own boundingRect().

    Your paint() should say:
    Qt Code:
    1. void ArmoredVehicle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. Q_UNUSED(option);
    4. Q_UNUSED(widget);
    5. painter->setPen(Qt::NoPen);
    6. painter->setBrush(Qt::blue);
    7. painter->drawPixmap(QPointF(-20, -20), QPixmap(3,30));
    8.  
    9. painter->setPen(Qt::NoPen);
    10. painter->setBrush(Qt::green);
    11. painter->drawPixmap(QPointF(-20,-20), QPixmap(40,20));
    12. }
    To copy to clipboard, switch view to plain text mode 
    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.


  8. #8
    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
    That's because you still don't understand the concept of a local coordinate system. Your item only knows of its own coordinate system and not of its parents. Consider a real world example - we're living on Earth which is part of the Solar system. When you are calculating distances or pointing places, you do it in Earth's coordinate system without taking into consideration that Earth is constantly revolving around the Sun (which is revolving around the centre of the Milky Way, etc.).
    I think that I understand the theory, but not the implementation.

    Ok, lets start from the beginning. Correct me if I'm wrong.

    All (cartesian) coordinate systems have top left corner coordinate of 0,0?
    All (cartesian) coordinate systems have bottom right corner coordinate of width,height?
    QPointF(0,0) is the center coordinate (ie. width/2, height/2) in the coordinate system?
    QPointF(-width/2,-height/2) is the top left corner (ie. 0,0)?

    Qt Code:
    1. QRectF ArmoredVehicle::boundingRect() const
    2. {
    3. return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
    4. }
    To copy to clipboard, switch view to plain text mode 

    This rectangle contains the item, right?
    Rectangle's center coordinates in the scene's coordinates are item->pos()?

    Another thing that I don't understand is that the unit doesn't move to the point of the click, but there is a seemingly random offset. Also when I resize the application window, the scene gets scroll bars, but it also loses it's size. So when the window is maximized the bottom left corner of the scene is for example x,y=1000,800, but when I halve the size of the window the x,y=500,400. Is there someway to prevent this scaling of the scene?

    And I still can't get any position data of where the item is in the scenes coordinates.
    Last edited by qtgames; 17th October 2009 at 16:54.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    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
    All (cartesian) coordinate systems have top left corner coordinate of 0,0?
    No.

    All (cartesian) coordinate systems have bottom right corner coordinate of width,height?
    No

    QPointF(0,0) is the center coordinate (ie. width/2, height/2) in the coordinate system?
    This is contradictory to your first statement

    QPointF(-width/2,-height/2) is the top left corner (ie. 0,0)?
    In this particular case - yes.

    Qt Code:
    1. QRectF ArmoredVehicle::boundingRect() const
    2. {
    3. return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
    4. }
    To copy to clipboard, switch view to plain text mode 

    This rectangle contains the item, right?
    Right.

    Rectangle's center coordinates in the scene's coordinates are item->pos()?
    In this particular case - yes.

    Is there someway to prevent this scaling of the scene?
    The scene is not scaled unless you scale it yourself. It is centred in the viewport if the latter is bigger than the current projection of the scene - maybe that's the effect you observe.

    And I still can't get any position data of where the item is in the scenes coordinates.
    It's in QGraphicsItem::pos()
    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.


  10. #10
    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, I'm totally lost now, but lets continue.

    What is the top left corner coordinate?
    What is the bottom right corner coordinate?
    Where is QPointF(0,0)?
    Where is QPointF(-width/2,-height/2)?

    The scene is not scaled unless you scale it yourself. It is centred in the viewport if the latter is bigger than the current projection of the scene - maybe that's the effect you observe.
    So how can I set the scale to be fixed without making the parent widget (window) fixed?

    item->pos().x() in the BattleField class, it gives out zero at all times. Also for some reason the application doesn't repaint item and remove (or do I have to repaint the background from the previous position?) the old one.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    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
    What is the top left corner coordinate?
    boundingRect().topLeft()

    What is the bottom right corner coordinate?
    boundingRect().bottomRight() which is the same as boundingRect().topLeft() + boundingRect().size()

    Where is QPointF(0,0)?
    It depends on what boundingRect() returns

    Where is QPointF(-width/2,-height/2)?
    It also depends on the bounding rect (its top left corner, to be exact).


    So how can I set the scale to be fixed without making the parent widget (window) fixed?
    "Fixed" to what? Fixed size? Just don't resize it and it will have constant size.


    item->pos().x() in the BattleField class, it gives out zero at all times.
    Did you use QGraphicsItem::setPos() prior to calling QGraphicsItem::pos()? If not, pos() will obviously return QPointF(0,0) which is the default position of every item relative to its parent.

    Also for some reason the application doesn't repaint item and remove (or do I have to repaint the background from the previous position?) the old one.
    Because your boundingRect() is inconsistent with the area of the viewport where you are trying to paint the item. You may only paint within boundingRect() of the item.
    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.


  12. #12
    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

    Double post.
    Last edited by qtgames; 17th October 2009 at 20:52.

  13. #13
    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
    It depends on what boundingRect() returns
    How does it depend on boundingRect?

    Quote Originally Posted by wysota View Post
    "Fixed" to what? Fixed size? Just don't resize it and it will have constant size.
    Yes, fixed size. But what if the player does?

    Quote Originally Posted by wysota View Post
    Did you use QGraphicsItem::setPos() prior to calling QGraphicsItem::pos()?
    Yes. The code I used was following, (vehicle inherits QGraphicsItem)

    Qt Code:
    1. vehicle->setPos(QPointF(event->x(),event->y()));
    To copy to clipboard, switch view to plain text mode 

    The unit does move with an offset that I have been unable to decypher, but

    Qt Code:
    1. qDebug("VehX after %d", vehicle->pos().x());
    To copy to clipboard, switch view to plain text mode 

    =0.

    Quote Originally Posted by wysota View Post
    Because your boundingRect() is inconsistent with the area of the viewport where you are trying to paint the item. You may only paint within boundingRect() of the item.
    So by item->setPos(newPoint) I move the items boundingRect in the scene?
    Then I call the item->update(item->boundingRect?), which should automatically call paint-function?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    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 does it depend on boundingRect?
    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).

    Yes, fixed size. But what if the player does?
    The player can only resize the view, not the scene.

    Yes. The code I used was following, (vehicle inherits QGraphicsItem)

    Qt Code:
    1. vehicle->setPos(QPointF(event->x(),event->y()));
    To copy to clipboard, switch view to plain text mode 

    The unit does move with an offset that I have been unable to decypher, but

    Qt Code:
    1. qDebug("VehX after %d", vehicle->pos().x());
    To copy to clipboard, switch view to plain text mode 

    =0.
    Please provide a minimal compilable example reproducing this behaviour.

    So by item->setPos(newPoint) I move the items boundingRect in the scene?
    Sort of... You move the origin of the item and as the boundingRect is defined in the same coordinate system as the origin, it also "moves". Compare the results of this sequence of calls:
    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 

    Then I call the item->update(item->boundingRect?), which should automatically call paint-function?
    No, by moving the item you ask graphics view to update the item - it will repaint both the old projection of the bounding rect to the scene and new projection of the bounding rect to the scene. If you paint outside the bounding rect, you'll get artifacts.
    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.


  15. #15
    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

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    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.


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

    qtgames (18th October 2009)

  18. #17
    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.

  19. #18
    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 10:06.

  20. #19
    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?

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    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, 21:09
  2. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 21:39
  3. Replies: 4
    Last Post: 19th February 2009, 12:10
  4. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 18:31
  5. A form returning an object.
    By cbarmpar in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 06: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.