Results 1 to 4 of 4

Thread: QGraphicsRectItem positioning problem

  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 10:12. Reason: missing [code] tags

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

    Default Re: QGraphicsRectItem positioning problem

    It would be nice if you provided a minimal compilable example reproducing the problem.
    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. #3
    Join Date
    Jul 2009
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsRectItem positioning problem

    Unfortunately I tried to do this(reproduce the problem in a separate program) before posting, but with no success. And I cannot post the current application cause it's from work and I am not allowed to .
    Also, I am aware of the fact that it might be a problem specific to my application.

    Opened for any suggestion, meanwhile I started overriding mouseMoveEvent

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

    Default Re: QGraphicsRectItem positioning problem

    Quote Originally Posted by gufeatza View Post
    Unfortunately I tried to do this(reproduce the problem in a separate program) before posting, but with no success.
    Which probably means the problem is elsewhere. Try comparing the code you have written to reproduce the problem with your actual code and try to find the part that makes the difference. That's the main point of asking for a compilable example reproducing the problem - in many cases users notice the problem by themselves.
    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. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 13:05
  2. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 15:38
  3. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 13:48
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 22:36
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 23: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.