Unsolved Printing QImage to PDF
-
Hi,
I use Qt for printing some graphics to PDF. Unfortunately, some QImages are not rendered correctly, I don't get the expected pixel colours.
I would expect the following code (adapted from this example https://wiki.qt.io/Exporting_a_document_to_PDF) to print a vertical image composed of 3 pixels : blue, green and red from top to bottom. Instead I get 3 pixels blue(Ok), light greenish, greenish (see attached pdf [0_1535805235367_toto.pdf](Uploading 100%) )
#include <QApplication> #include <QtCore> #include <QPrinter> #include <QImage> #include <QPrinter> #include <QPixmap> #include <QPainter> #include <QFileDialog> int main(int argc, char *argv[]) { QApplication app(argc, argv); QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf"); if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); } QPrinter printer(QPrinter::PrinterResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName(fileName); QPainter painter; painter.begin (&printer); QImage img (1, 3, QImage::Format_ARGB32); img.setPixelColor (0,0,QColor (Qt::red)); img.setPixelColor (0,1,QColor (Qt::green)); img.setPixelColor (0,2,QColor (Qt::blue)); // Increase the apparent size of the image QTransform tform(55.8,0,0,-55.8,74.88,236.34); painter.setTransform (tform); painter.drawImage (QRect (0,0,1, 3), img); painter.end (); }
What am I doing wrong?
Thanks,
Pantxo
-
Oh, and if I modify the original example (see below) to print a 6 pixels image (red, red, green, green, blue, blue), then I get a better result but note that the two blue pixels don't have the exact same colour (see pdf attached)[0_1535807541749_toto.pdf](Uploading 100%) .
#include <QApplication> #include <QtCore> #include <QPrinter> #include <QImage> #include <QPrinter> #include <QPixmap> #include <QPainter> #include <QTextDocument> #include <QFileDialog> int main(int argc, char *argv[]) { QApplication app(argc, argv); QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf"); if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); } QPrinter printer(QPrinter::PrinterResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName(fileName); QPainter painter; painter.begin (&printer); QImage img (1, 6, QImage::Format_ARGB32); img.setPixelColor (0,0,QColor (Qt::red)); img.setPixelColor (0,1,QColor (Qt::red)); img.setPixelColor (0,2,QColor (Qt::green)); img.setPixelColor (0,3,QColor (Qt::green)); img.setPixelColor (0,4,QColor (Qt::blue)); img.setPixelColor (0,5,QColor (Qt::blue)); // Increase the apparent size of the image QTransform tform(58.68,0,0,-58.68,268.74,384.48); painter.setTransform (tform); painter.drawImage (QRect (0,0,1, 6), img); painter.end (); }
In case it may be relevant I use Qt 5.9 on linux Mint 19.
Thanks
-
Nevermind, I found out that this is an already reported bug :
https://bugreports.qt.io/browse/QTBUG-54809
Pantxo