Qt 如何使用“中心对齐”在Qpixmap上绘制文本

Qt 如何使用“中心对齐”在Qpixmap上绘制文本,qt,Qt,油漆; drawText(w,h,Qt::AlginCenter,“Hello”) 给出错误,无加工功能。您应在构造函数或begin函数中将QPixmap传递给QPainter: QPainter painter; painter.drawText(QRect(0, 0, w, h), Qt::AlignCenter, "Hello"); QPixmap pixmap; // ... QPainter painter( &pixmap ); // OR QPainter paint

油漆; drawText(w,h,Qt::AlginCenter,“Hello”)


给出错误,无加工功能。

您应在构造函数或begin函数中将QPixmap传递给QPainter:

QPainter painter;
painter.drawText(QRect(0, 0, w, h), Qt::AlignCenter, "Hello");
QPixmap pixmap;
// ...

QPainter painter( &pixmap );

// OR
QPainter painter;
painter.begin( &pixmap );
然后,您应选择以下选项之一:

void drawText( const QRectF& rectangle, int flags, const QString& text, QRectF* boundingRect = 0 )
void drawText( const QRect& rectangle, int flags, const QString& text, QRect* boundingRect = 0 )
void drawText( int x, int y, int width, int height, int flags, const QString& text, QRect* boundingRect = 0 )
因此,如果要定义对齐方式,需要传递一个要在其中绘制文本的矩形


您忘记了定义矩形的左上角,只是将宽度和高度参数传递给了draw函数。

正在寻找一种将文本居中的方法,这非常有效!