#include <QApplication>
#include <QLabel>
#include <QTextCodec>
#include <QRgb>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QTextCodec *codec = QTextCodec::codecForName("Big5-ETen");
QLabel *label = new QLabel;
label->setText(codec->toUnicode("<center><h1>Qt4 學習筆記</h1>~yycking.blogspot.com</center>"));
label->setStyleSheet("QLabel { background-color: white; }");
label->adjustSize();
label->show();
QImage image = QPixmap::grabWidget(label).toImage();
image = image.convertToFormat(QImage::Format_Indexed8);
QVector<QRgb> indexs=image.colorTable();
for (int i = 0; i < indexs.size(); ++i) {
int r=qRed(indexs[i]);
int g=qGreen(indexs[i]);
int b=qBlue(indexs[i]);
int a=qGray(indexs[i]);
indexs[i]=qRgba(r,g,b,255-a);
}
image.setColorTable(indexs);
image.save("somefile.PNG");
return app.exec();
}
這個方法只簡易的將Gray代替alpha而已的圖