我有一段用于绘图的代码。使用诺基亚Qt导出透明图像。但是,它不起作用。我总是看到有很多噪音的黑色背景。我必须用白色填充背景,但我想要透明度,而不是白色。请劝告。
#include <QtGui/QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
{
QImage pix(260, 50, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&pix);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.setPen(QColor(0x00, 0x00, 0x00, 0xff));
painter.setBrush(QColor(0x00, 0x00, 0x00, 0xff));
painter.setFont(QFont("AvantGarde Md BT", 32));
painter.setBackgroundMode(Qt::TransparentMode);
// I don't like the white color, but I must put otherwise you'll see dumb & black background
painter.fillRect(0, 0, pix.width(), pix.height(), QColor(0xff, 0xff, 0xff, 0xff));
QString text("My Text");
QFontMetrics fm(painter.font());
int w = fm.width(text);
int h = fm.height();
painter.drawText((pix.width() - w)/2, (pix.height() + h/2)/2, text);
pix.save(QString("mytext.png"));
}
}
答案 0 :(得分:4)
我自己找到答案:
我必须在声明pix之后立即添加一条魔术线:
pix.fill(Qt::transparent);
<强> 参考文献: 强>
http://techbase.kde.org/Development/Tutorials/Graphics/Performance
http://www.informit.com/articles/article.aspx?p=1174421&seqNum=3