Dennis
Dennis

Reputation: 186

Qt child QGraphicItem does not use parent coordinate system

I'm trying to create a BuildingTile class which has QGraphicsRectItem as base.

In this BuildingTile I'm trying to add QGraphicsEllipseItems and a QGraphicsSimpleTextItem but these do not use my BuildingTile's coordinate system although they say on http://doc.qt.io/qt-5/graphicsview.html: "Child coordinates are relative to the parent's coordinates. If the child is untransformed, the difference between a child coordinate and a parent coordinate is the same as the distance between the items in parent coordinates."

I would be really glad if someone could help me with this.

http://i.imgur.com/5B0ISLD.png

The header:

class BuildingTile : public QGraphicsRectItem
{

private:
    Building* m_building;

    bool m_empty;
    QGraphicsSimpleTextItem* m_name;
    QList<QGraphicsEllipseItem*> m_colonists;
public:
    BuildingTile(qreal x, qreal y, QColor color, QString name, Building* m_building = 0);

    bool isEmpty() const {return m_empty;}
    void setEmpty(bool empty) {m_empty = empty;}
    void setBuilding(Building* building) {m_building = building;}
};

The constructor:

BuildingTile::BuildingTile(qreal x, qreal y, QColor color, QString name, Building *building) : QGraphicsRectItem(x,y,150,75)
{
    m_building = building;
    setBrush(color);
    for(int i = 0; i<3; i++)
    {
        QGraphicsEllipseItem* item = new QGraphicsEllipseItem(10+i*35, 40, 25, 25, this);
        m_colonists.append(item);
        item->setBrush(QColor(255,255,255));
    }
    m_name = new QGraphicsSimpleTextItem(name, this);
    m_name->setPos(10,10);
}

MainWindow constructor:

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    QGraphicsScene* scene = new QGraphicsScene;
    BuildingTile* item = new BuildingTile(0, 0, QColor(203,130,232), "small market");
    scene->addItem(item);
    item = new BuildingTile(150, 0, QColor(91,161,212), "indigo plant");
    scene->addItem(item);
    item = new BuildingTile(300, 0, QColor(120,113,107), "coffee roaster");
    scene->addItem(item);

    QGraphicsView* view = new QGraphicsView;
    view->setScene(scene);
    view->setAlignment(Qt::AlignTop | Qt::AlignLeft);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(view);
    setLayout(layout);
}

Upvotes: 0

Views: 421

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25165

All your BuildingTile items have their origin at the scene's origin, i.e. (0, 0) in scene coordinates.

For example (your second BuildingTile item):

item = new BuildingTile(150, 0, QColor(91,161,212), "indigo plant");
scene->addItem(item);

This creates a BuildingTile item located at (0, 0), containing a rectangle located at (150,0) of its own coordinate system. You're changing the position of the rectangle in its own coordinate system, but not the position of the rect's coordinate system in relation to its parent (the scene).

Now you create ellipses and labels in relation to the BuildingTile coordinate systems, which are all identical and located at (0,0) in "global" scene coordinates, so you end up with scene coordinates (10, 10) for all labels.

To achieve what you want, do:

item = new BuildingTile(0, 0, QColor(91,161,212), "indigo plant");
scene->addItem(item);
item->setPos(150, 0);

Upvotes: 1

Related Questions