2

Here is part of the code, when sending x=0 and y=0, it places the child item "text" in the (0,0) of the scene not relative to its parent !

void ClassA::putLabel(qreal x, qreal y, QString text, QGraphicsPolygonItem* parent) {

    QGraphicsTextItem * io = new QGraphicsTextItem;
    io->setParentItem(parent);
    io->setPos(x, y);
    io->setFlags(QGraphicsItem::ItemIgnoresTransformations);
    //io->setHtml("<div style='font-size: 10px; padding: -2px; margin: -2px;'>" + text + "</div>");
    io->setFont(QFont("Helvetica", 8));
    io->setPlainText(text);
}
Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
  • The code you've posted looks ok to me. Have you checked the x and y coordinates being passed in? It shouldn't make a difference, but what happens if you don't ignore the item transformations? – TheDarkKnight Jan 09 '14 at 10:31
  • Did you check whether the `parent` is not null? – vahancho Jan 09 '14 at 10:49
  • @Merlin069 I commented the line of ItemIgnoresTransformations but the same happened. x and y are passed correctly when I send (0,0) it shows in the upper left and when I pass other values it shows in the correct position but relative to the whole scene. – Mahmoud Hassan Jan 09 '14 at 11:03
  • @vahancho Parent is not NULL, I checked, moreover when I hide() the parent, the child label get hidden too. – Mahmoud Hassan Jan 09 '14 at 11:04
  • What about if you add the text item to the scene, then set its parent? – TheDarkKnight Jan 09 '14 at 11:17
  • if some item (parent for example) uses translations in transformations then this line `io->setFlags(QGraphicsItem::ItemIgnoresTransformations);` can modify item position. – Marek R Jan 09 '14 at 11:51
  • @MarekR well , not sure how this relate to the problem, I want when setting x=0 and y=0, the child item be in the top left corner of the parent item. – Mahmoud Hassan Jan 09 '14 at 13:53
  • you mean add the item to scene first? setParent() adds the child item to the same scene as its parent automatically. – Mahmoud Hassan Jan 09 '14 at 14:08
  • @MahmoudHassan if one of the parents, uses [transformation](http://qt-project.org/doc/qt-5.0/qtwidgets/qgraphicsitem.html#setTransform) then this flag is very important for final item position. There is no other explanation why your code (which you have shown) doesn't work. If this is not a problem, then bug is in some other code not included to question. – Marek R Jan 09 '14 at 14:09
  • @MarekR, according to the docs for ItemIgnoresTransformations: "its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored". So it shouldn't affect the translation of the child relative to its parent. I agree that it's likely some other code, not displayed in the question, that is causing the issue. – TheDarkKnight Jan 09 '14 at 14:38

0 Answers0