void MainWindow::drawText(int font_size, QString str, int n)
{
QPainter p;
QSize size(460, font_size); //指定图片大小;
QImage* image = new QImage(size, QImage::Format_ARGB32); //以ARGB32格式构造一个QImage,
image->fill(qRgba(0,0,0,0));

p.begin(image);
QPen pen = p.pen();
pen.setColor(Qt::red);
QFont font;
font.setFamily(("SimSun"));
font.setPixelSize(64);//改变字体大小

p.setPen(pen);
p.setFont(font);
p.setBackgroundMode(Qt::TransparentMode);//Qt::OpaqueMode , TransparentMode
p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
p.drawText(image->rect(), Qt::AlignCenter, QObject::tr(str.toStdString().c_str())); //Qt::AlignLeft Qt::AlignRight

if(1 == n)
{
image->save("1.bmp");
}
else
{
image->save("2.bmp");
}

p.end();

}

posted on 2016-05-20 17:22  邶风  阅读(869)  评论(0编辑  收藏  举报