PDA

View Full Version : QGraphicsText parent item on top of QGraphicsRectItem



^NyAw^
23rd February 2016, 17:54
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?



QGraphicsScene *pqScene = new QGraphicsScene(this);
ui.graphicsView->setScene(pqScene);

QGraphicsTextItem *pqText = new QGraphicsTextItem();
pqText->setPlainText("Hello");
//QGraphicsTextItem *pqText = pqScene->addText("Hello"); //Tried this with same result
pqText->setZValue(0);
pqText->setFlags(QGraphicsItem::GraphicsItemFlag::ItemIsMo vable);

QGraphicsRectItem *pqRect = new QGraphicsRectItem(pqText);
//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,

anda_skoa
23rd February 2016, 19:42
Maybe put both as sibling into the same parent item?
E.g. a QGraphicsItemGroup?

Cheers,
_

^NyAw^
24th February 2016, 09:51
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,

anda_skoa
24th February 2016, 11:26
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,
_

^NyAw^
24th February 2016, 11:53
Hi,



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
11736

This is what I want
11737

^NyAw^
25th February 2016, 09:45
So,

Am I doing it wrong or is a Qt bug?

anda_skoa
25th February 2016, 10:23
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:


You can set the ItemStacksBehindParent flag to stack a child item behind its parent.


Cheers,
_

^NyAw^
25th February 2016, 10:37
Hi,




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.