Results 1 to 4 of 4

Thread: QGraphicsRectItem positioning problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsRectItem positioning problem

    Hello, I come to you with a bothering problems I met.

    I am trying to create and use an object derived from QGraphicsRectItem inside a QGraphicsWidget by the next method: I create it with the current widget as parent parameter, but in a dynamic way – as dragging the mouse throughout the scene, the object is expanding until the mouse is released(consecutive setRect methods applied to the item in mouseMoveEvent handle).

    Afterward I attach the newly created object to the current scene.

    My object overrides the method itemChange so when the change ItemPositionChange materializes, I do the classic object positioning for it to be limited in the scene's rect as it follows:

    Quoted
    Qt Code:
    1. QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if (change == ItemPositionChange && scene()) {
    4. // value is the new position.
    5. QPointF newPos = value.toPointF();
    6. QRectF rect = scene()->sceneRect();
    7. if (!rect.contains(newPos)) {
    8. // Keep the item inside the scene rect.
    9. newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
    10. newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
    11. return newPos;
    12. }
    13. }
    14. return QGraphicsItem::itemChange(change, value);
    15. }
    To copy to clipboard, switch view to plain text mode 


    The problem is, as you guessed, that the method doesn't do it's job(it acts as if the scene's origin point, which is (0, 0) in the viewport, would be somewhere in the middle of that scene so the rect item's movement being limited by that point). So I tried to apply a mapToScene to that point, but seems that the returned value still isn't as it should be.

    To be more clear, I set the item's position in the scene to the point (0, 0) and when ItemPositionChange shows up, it's value hits me with a point that does not map correctly to the scene.

    So, let me know if anything is wrong in my thinking (God help), cause I believe if it's an anomaly I must override the method that handles the mouseMoveEvent to move the QGraphicsRectItem manually.

    Thanks a lot!!
    Last edited by wysota; 4th September 2009 at 09:12. Reason: missing [code] tags

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 14:38
  3. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

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.