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