Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the coordinate of QGraphicsEllipseItem's object ?

How to get the coordinate of QGraphicsEllipseItem's object ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    romeo.rbw
    wrote on last edited by
    #1

    Hi..
    I try to get the coordinate of a moving circle that was made using QGraphicsEllipseItem on the scene, but the result always (0,0). I appreciate if there is any comment or suggestion, thank you...
    @
    ... QGraphicsEllipseItem *myCircleItem = new QGraphicsEllipseItem(
    QRect(550,200,50,50));
    myCircleItem->setPen(QPen(Qt::black));
    myCircleItem->setBrush(Qt::blue);
    myCircleItem->setFlags (QGraphicsItem::ItemIsMovable);

    scene->addItem(myCircleItem);
    
    posisiCircleX = myCircleItem->pos().x();
    posisiCircleY = myCircleItem->pos().y();
    qDebug() << "coordinate myCircelItem (x,y) : " << posisiCircleX <<" , "<< posisiCircleY;
    

    ...
    @

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      and you're calling pos() also after it has been moved?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        There's two things here:
        The position - pos(), which you change using setPos. This is the position of the whole item relative to it's parent.

        And the rectangle - rect() which you change using setRect. This contains both the offset and size of the ellipse, relative to the position given.
        In your case, you only specify a rectangle, therefore the position is always zero.

        How to use correctly:
        Think about where you'd like to have the "origin point" of your ellipse or circle. In the center? In this case, use something like this:
        @
        pEllipseItem->setRect(-20.0, -20.0, 40.0, 40.0);
        pEllipseItem->setPos(50.0, 80.0);@

        Now the center of the circle (in this case) will be at 50.0 / 80.0

        1 Reply Last reply
        0
        • R Offline
          R Offline
          romeo.rbw
          wrote on last edited by
          #4

          Thanks that is solved using setRect and setPos as your suggestion.
          Now, the coordinate not (0,0) again. :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved