QGraphicsText parent item on top of QGraphicsRectItem
Hi,
I have a QGraphicsScene with a QGraphicsTextItem and a QGraphicsRectItem as child of the text item.
What I want is the text item to be displayed on top of the rect item and move the rect item when the text is moved.
The problem is that the rect item is always displayed on top of the text item. The only way to display the text on top of the rect item is to not set the text item as parent of the text rect(then the two items are independents and this is not what I want).
Am I missing something?
Code:
ui.graphicsView->setScene(pqScene);
pqText->setPlainText("Hello");
//QGraphicsTextItem *pqText = pqScene->addText("Hello"); //Tried this with same result
pqText->setZValue(0);
pqText
->setFlags
(QGraphicsItem::GraphicsItemFlag::ItemIsMovable);
//QGraphicsRectItem *pqRect = pqScene->addRect(pqText->boundingRect()); //Tried this with same result
//pqRect->setParentItem(pqText);
pqRect->setBrush(Qt::red);
pqRect->setZValue(-1);
pqRect->setRect(pqText->boundingRect());
pqScene->addItem(pqText);
pqScene->addItem(pqRect);
Using Qt 5.4.1 on Windows with Visual Studio 2013.
Thanks,
Re: QGraphicsText parent item on top of QGraphicsRectItem
Maybe put both as sibling into the same parent item?
E.g. a QGraphicsItemGroup?
Cheers,
_
Re: QGraphicsText parent item on top of QGraphicsRectItem
Hi,
I created a QGraphicsTextItem derivred class that has a QGraphicsRectItem as child. When the text item is modified(font, text,...) the child rect item modifies its rect to the text bounding rectangle.
Is this not a good approach? So, what is the parent-child relationship used for? I thought it was for something like this.
Another solution could be to use the paint method of the text derived class to paint the bounding rect and then the text, but by the moment I want to understand what I'm doing wrong.
Thanks,
Re: QGraphicsText parent item on top of QGraphicsRectItem
Quote:
Originally Posted by
^NyAw^
I created a QGraphicsTextItem derivred class that has a QGraphicsRectItem as child. When the text item is modified(font, text,...) the child rect item modifies its rect to the text bounding rectangle.
Is this not a good approach?
If it does what you need then it is a good approach.
Cheers,
_
2 Attachment(s)
Re: QGraphicsText parent item on top of QGraphicsRectItem
Hi,
Quote:
If it does what you need then it is a good approach.
No, it doesn't.
My example code shows that the rect item is ALWAYS displayed on top of the text item(the text is not visible) and I'm not able to display the text item on top of the rect item.
This is what I get
Attachment 11736
This is what I want
Attachment 11737
Re: QGraphicsText parent item on top of QGraphicsRectItem
So,
Am I doing it wrong or is a Qt bug?
Re: QGraphicsText parent item on top of QGraphicsRectItem
Quote:
Originally Posted by
^NyAw^
Am I doing it wrong or is a Qt bug?
Can easily be answered by looking at the code :-)
In this case even by looking into the documentation:
Quote:
Originally Posted by The GraphicsItem documentation
You can set the ItemStacksBehindParent flag to stack a child item behind its parent.
Cheers,
_
Re: QGraphicsText parent item on top of QGraphicsRectItem
Hi,
Quote:
In this case even by looking into the documentation:
Quote Originally Posted by The GraphicsItem documentation
You can set the ItemStacksBehindParent flag to stack a child item behind its parent.
This works perfectly. I thought that changing the z value of the item will do the same.