Buggy behaviour of setPos of QGraphicsSimpleTextItem



  • I have a zoom button(QGraphicsRectItem) which has two QGraphicsSimpleTextItem (+,-) as children. When i call setPos on text items coordinates are always set in scene's coordinates despite the fact that text items have parent(zoom button).

    This behaviour is against the documentation:

    "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. For example: If an untransformed child item is positioned precisely in its parent's center point, then the two items' coordinate systems will be identical. If the child's position is (10, 0), however, the child's (0, 10) point will correspond to its parent's (10, 10) point.Because items' position and transformation are relative to the parent, child items' coordinates are unaffected by the parent's transformation, although the parent's transformation implicitly transforms the child"

    @
    ZoomButton::ZoomButton(GeoMap * map,QGraphicsItem * parent):QGraphicsRectItem(parent) {
    this->map = map;
    setPen(QPen(QColor(255,255,255)));
    setBrush(QBrush(QColor(0,0,0,150)));
    // plusText = new QGraphicsTextItem()
    plusText = new QGraphicsSimpleTextItem(this);
    minusText = new QGraphicsSimpleTextItem(this);
    plusText->setText("+");
    minusText->setText("-");
    plusText->setPen(QPen(QColor(255,255,255)));
    plusText->setBrush(QBrush(QColor(255,255,255)));
    minusText->setBrush(plusText->brush());
    minusText->setPen(plusText->pen());
    QFont f;
    f.setFamily("Arial");
    f.setFixedPitch(true);
    f.setPixelSize(40);
    plusText->setFont(f);
    minusText->setFont(f);

    plusText->setPos(0,0); // it's just for this sample
    minusText->setPos(40,0);
    }

    @

    @
    QGraphicsScene * sc = new QGraphicsScene();
    zoomButton = new ZoomButton(map);
    zoomButton->setRect(100,100,200,70);
    sc->addItem(zoomButton);
    sc->setBackgroundBrush(QBrush(QColor(30,100,100)));
    view = new QGraphicsView(sc,0);
    view->setSceneRect(0,0,1000,1000);

    @



  • Did you consider the origin of your 'ZoomButton' object is (200,135) (center of rect) (not (100,100) which is top left) in scene corrdinate? In my case, the actual origin has been always easily misunderstood by myself. :-(


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.