Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt allignment of a QGraphicsSimpleTextItem in a QGraphicsScene

Tags:

qt5.5

I want to add a simple text item (one word) to a graphics scene and so use QGraphicsSimpleTextItem. The scene coordinates of the text item defines the upper left corner of the text.

Is it possible to have the text center alligned around a coordinate?

like image 807
Jan Laloux Avatar asked Mar 15 '23 15:03

Jan Laloux


1 Answers

Use setPos() for this. For example, where (X,Y) is the center point to which the text must be aligned:

QGraphicsSimpleTextItem *Item;
Item->setText( QString::fromLatin1( "My text" ) );
QRectF bR = Item->sceneBoundingRect();
Item->setPos( X - bR.width()/2, Y - bR.height()/2 );
like image 106
Jan Laloux Avatar answered Jun 14 '23 01:06

Jan Laloux