Forcing QGraphicsItem to post

I have a QGraphicsScene that is populated using QGraphicsItems , and the user can pan and zoom to check all the different QGraphicsItems . However, I would like to have a QGraphicsTextItem label that always remains in the upper left corner of the screen. Ignoring scaling is very simple since I can just setFlags(QGraphicsItem::ItemIgnoresTransformations) , but I still have to figure out how to make the position stay in the upper left corner. What is the best way to make my shortcut β€œfloat” in the same place?

+6
c ++ qt qt4 qgraphicsview qgraphicsitem
source share
2 answers

Perhaps you just want to correctly place QLabel on the view instead of trying to make part of the scene in one place, as discussed in this question .

Note. As mentioned in another answer, this trick often does not work with hardware accelerated representations. In these cases, you will need to make text elements part of the scene.

+3
source share

I think the only (real) way to do this is to recalculate the position for your text element in each frame. To do this, simply subclass QGraphicsView, override paintEvent and use QGraphicsView :: mapToScene () to calculate the new position. Something like:

 void MyGraphicsView::paintEvent(QPaintEvent*) { QPointF scenePos = mapToScene(0,0); // map viewport top-left corner to scene m_textItem->setPos(scenePos); } 

I have done this many times and it worked very well.

Of course, you could just create a normal QLabel, as Arnold Spence mentions in his answer. However, this will not work in many situations (for example, if you really want to place the label on top of the graphical representation and you use the OpenGL-accelerated viewport).

+6
source share

All Articles