2015-05-04 94 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) 

所以,如果你想要定義您需要傳遞要在其中繪製文本的矩形的對齊方式。

您忘記定義矩形的左上角,只是將寬度和高度參數傳遞給繪圖函數。