QGraphicsScene 中 QGraphicsSimpleTextItem 的 Qt 对齐

Jan*_*oux 3 qt5.5

我想向图形场景添加一个简单的文本项(一个字),因此使用 QGraphicsSimpleTextItem。文本项的场景坐标定义了文本的左上角。

是否可以让文本中心围绕坐标对齐?

Jan*_*oux 6

为此使用 setPos()。例如,其中 (X,Y) 是文本必须对齐的中心点:

QGraphicsSimpleTextItem *Item;
Item->setText( QString::fromLatin1( "My text" ) );
QRectF bR = Item->sceneBoundingRect();
Item->setPos( X - bR.width()/2, Y - bR.height()/2 );
Run Code Online (Sandbox Code Playgroud)