[Qt-interest] Problem with QGraphicsScene/view/item coordinates

Kimmo Viitanen kviitanen at gmail.com
Sun Aug 8 11:16:25 CEST 2010


Hello,

I create a QGraphicsRectItem at position (200, 350) with a size of (10, 10).
I insert this item to a scene using addItem(). When I retrieve the item
parent's coordinates with QGraphicsItem::pos() I get (0, 0) as a result.
Yet the view I have attached to the scene shows the items at correct
positions. So what's going on? How can I get the coordinates of an item
in the scene coordinates?

The docs say that the scene is the parent for all parentless items and
that pos() returns the items coordinates in the parent's coordinate system.
So by calling pos() I'd expect to get back (200, 350).

Using Qt 4.6.1.

This is how I create the scene, items and set the scene:

    scene = new QGraphicsScene;
    scene->setBackgroundBrush(QBrush(QColor(240, 250, 240)));

    // Create the items.
    aimer = new QGraphicsRectItem(200, 350, 10, 10);
    aimer->setBrush(QBrush(QColor(Qt::black)));

    target1 = new QGraphicsRectItem(50, 10, 10, 10);
    target1->setBrush(QBrush(QColor(Qt::red)));

    target2 = new QGraphicsRectItem(30, 100, 10, 10);
    target2->setBrush(QBrush(QColor(Qt::magenta)));

    target3 = new QGraphicsRectItem(40, 400, 10, 10);
    target3->setBrush(QBrush(QColor(Qt::gray)));

    // Insert items to scene
    scene->addItem(aimer);
    scene->addItem(target1);
    scene->addItem(target2);
    scene->addItem(target3);

    // Set scene for the view
    ui->graphicsView->setScene(scene);



More information about the Qt-interest-old mailing list