我有一段代码用于使用诺基亚 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"));
}
}
最佳答案
我自己找到了答案:
我只需要在 pix 声明之后添加一条魔术线:
pix.fill(Qt::transparent);
引用:
http://techbase.kde.org/Development/Tutorials/Graphics/Performance
http://www.informit.com/articles/article.aspx?p=1174421&seqNum=3
关于c++ - 如何使用诺基亚 Qt C++ 绘制和导出透明图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2018980/