views:

585

answers:

1

I have a piece of code intended for drawing & exporting a transparent image with Nokia Qt. However, it does not work. I always see black background with lots of noise. I must fill the background with White color but I want transparency, not white color. Please kindly advise.

#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"));
    }
}
+3  A: 
Viet