2015-05-04 93 views

回答

2
QPainter painter; 
painter.drawText(QRect(0, 0, w, h), Qt::AlignCenter, "Hello"); 
+0

正在寻找一种方式来居中文本,这就像一个魅力工作! – RandomGuy

3

您应在的QPixmap传递给QPainter的构造函数或开始功能:

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) 

所以,如果你想要定义您需要传递要在其中绘制文本的矩形的对齐方式。

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