Results 1 to 5 of 5

Thread: How can a QGraphicsRectItem get the coordinate of its top left corner?

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How can a QGraphicsRectItem get the coordinate of its top left corner?

    How can a QGraphicsRectItem retrieve the coordinate of its top left corner, relative to the scene it is associated with? I tried working with boundingRect().x(), but these coordinates are outdated after the item is dragged across the scene. scenePos().x() always returns 0 for some reason.

    I'm asking because I want to implement my own mousePressEvent on a class that derives from QGraphicsRectItem. I need to know if the user clicked in a corner of the rectangle or not so that I can allow the shape to be resized using selection grips.

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

    Default Re: How can a QGraphicsRectItem get the coordinate of its top left corner?

    scenePos().x() returns 0 because you specified the item's rectangle in scene coordinates instead of its local coordinates. Thus you have placed the item in its desired position not by moving the item in its appropriate place with setPos(). In either case mapToScene(boundingRect().topLeft()) will return the value you want bur you should first consider if you created the item correctly in the first place.
    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
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can a QGraphicsRectItem get the coordinate of its top left corner?

    The initializer list of the offending object contains a call to QGraphicsRectItem(x, y, width, height) where x and y are scene coordinates. Is this the wrong way to do it? The doc for QGraphicsRectItem is a little vague about this.

    The only other place where I modify the item's rectangle is when the user wants to change the rectangle's width and the height is changed proportionally:
    this->setRect(this->rect().x(), this->rect().y(), width, width/WH_RATIO);

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

    Default Re: How can a QGraphicsRectItem get the coordinate of its top left corner?

    Quote Originally Posted by blooglet View Post
    The initializer list of the offending object contains a call to QGraphicsRectItem(x, y, width, height) where x and y are scene coordinates. Is this the wrong way to do it? The doc for QGraphicsRectItem is a little vague about this.
    It depends what you mean by "wrong". Usually you'd want a rectangle that starts in (0,0) and expands to the right and down (i.e. has positive width and height) but specifying some other rectangle might be ok too in some situations.
    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.


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

    blooglet (13th February 2011)

  6. #5
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can a QGraphicsRectItem get the coordinate of its top left corner?

    You helped uncover some oddities in my code... my derivative of QGraphicsRectItem had a constructor that took the desired scene coordinates and passed them to the constructor of QGraphicsRectItem. Then I remembered that Qt's built-in graphic items didn't do that and the caller had to use newGraphicsItem->setPos() to set the position. I updated my code to fix this illogical behavior.

    Thanks for your help!

Similar Threads

  1. Replies: 4
    Last Post: 30th May 2010, 13:55
  2. Replies: 10
    Last Post: 29th May 2010, 19:42
  3. QTableWidget - Using top left corner cell
    By SteveH in forum Newbie
    Replies: 6
    Last Post: 12th March 2010, 20:33
  4. Replies: 1
    Last Post: 7th September 2008, 11:47
  5. Replies: 7
    Last Post: 15th November 2007, 18:19

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.