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. Position in scene coordinates
Forum Updated to NodeBB v4.3 + New Features

Position in scene coordinates

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 8.4k 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.
  • K Offline
    K Offline
    ksiedzulek
    wrote on last edited by
    #1

    Hi,

    I am learning about graphic view, I have written simple code to check how it behave and I don't know how to get position of items in scene coordinates:/

    @Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    scene = new QGraphicsScene(-200,-200,400,400);
    ui->graphicsView->setScene(scene);
    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackpen(Qt::black);
    blackpen.setWidth(6);
    rec=scene->addRect(175,-25,50,50,blackpen,blueBrush);
    scene->addRect(-225,-25,50,50,blackpen,blueBrush);
    scene->addRect(-25,175,50,50,blackpen,blueBrush);
    scene->addRect(-25,-225,50,50,blackpen,blueBrush);

    ellipse=scene->addEllipse(-50,-50,100,100,blackpen,redBrush);
    rec->setFlag(QGraphicsItem::ItemIsMovable);
    
    qDebug()<<ellipse->scenePos();
    qDebug()<<ellipse->pos();
    

    }@

    Qt documentation says about pos():

    bq. .., If the item has no parent, its position is given in scene coordinates.
    The position of the item describes its origin (local coordinate (0, 0)) in parent coordinates; this function returns the same as mapToParent(0, 0).
    For convenience, you can also call scenePos() to determine the item's position in scene coordinates,..

    but qDebug shows for both (pos and scenePos) "QPointF(0, 0)" I suppose its point from local scene.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr-Merlin
      wrote on last edited by
      #2

      Hi,

      When you add the rects with addRect you are defining the size of each rect, but not the position.

      The pos() function will give a position relative to the item's parent, which in the case of your code is the scene, so they're the same.

      If you create two rects A and B and make B a child of A, then add rect A to the scene, you can move B and see that the functions pos() and scenePos() are different.

      Hope that helps ;O)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        ksiedzulek
        wrote on last edited by
        #3

        ok, i'll check this:). it sounds good.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shoyeb
          wrote on last edited by
          #4

          also one thing to point out, u should not do all of your stuff in the constructor of your class....,

          There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

          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