QPdfWriter and Qpainter Position
-
I am using QPdfWriter to output pdf files and Qpainter to draw. The dpi affects the coordinate system of Qpainter, but I don't know how the coordinate system of Qpainter is converted.
QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile); pdfWriter->setPageSize(QPagedPaintDevice::A4); pdfWriter->setResolution(QPrinter::ScreenResolution); pdfWriter->setPageMargins(QMarginsF(0, 0, 0, 0)); QTextOption option(Qt::AlignCenter); option.setWrapMode(QTextOption::WordWrap); QFont font("Arial", 12); QPainter *pdfPainter = new QPainter(pdfWriter); pdfPainter->setFont(font); pdfPainter->drawText(QRect(500, 500, 2500, 500), "test", option);
The text should be drawn at x:500 y:500, but when I open the pdf file it is not at that position. How is this conversion done? How can I ensure that it is drawn at the correct position?
-
@Jun_ Actually I don't quite understand what is "correctly at 40,40". When setting 96dpi, drawing at 40,40 just means drawing at 0.42inch, 0.42inch.
pdfWriter->setResolution(QPrinter::ScreenResolution);
I don't know why are you keep writing like ⬆this, as I said this is invalid, it equals
pdfWriter->setResolution(0);
Anyway after calling that,
pdfWriter->resolution()
tells me the current resolution is 1200.
For 1200dpi, to draw at the same position as 96dpi, every value should * 12.5.
So 500,500 is the same position as 40,40 on 96dpi.pdfWriter->setResolution(96); pdfPainter->drawText(QPoint(40, 40), "test");
pdfWriter->setResolution(1200); pdfPainter->drawText(QPoint(500, 500), "test");
-
pdfWriter->setResolution(QPrinter::ScreenResolution);
This is wrong and invalid (because QPrinter::ScreenResolution = 0), so it will keep the default resolution which is 1200(dpi).
So for A4 size (8.26inch x 11.69inch), that will be about 9912 x 14028.QTextOption option(Qt::AlignCenter);
You draw the text in the center of QRect(500, 500, 2500, 500), so the text won't be at x:500 y:500.
If you want to test the coordinate, draw a rect bypdfPainter->drawRect(QRect(500, 500, 2500, 500));
-
@Bonnie Thanks for your reply.
What I mean is how to convert x, y, width, and height under the influence of different DPI.
exp:
pdfWriter->setResolution(96);
QTextOption option(Qt::AlignLeft);
pdfPainter->drawText(QRect(40, 40, 2500, 500), "test", option);
Can be drawn correctly at 40,40pdfWriter->setResolution(QPrinter::ScreenResolution);
QTextOption option(Qt::AlignLeft);
pdfPainter->drawText(QRect(x, y, width, height), "test", option);
x, y How to convert to achieve the same position effect as 40 -
@Jun_ Actually I don't quite understand what is "correctly at 40,40". When setting 96dpi, drawing at 40,40 just means drawing at 0.42inch, 0.42inch.
pdfWriter->setResolution(QPrinter::ScreenResolution);
I don't know why are you keep writing like ⬆this, as I said this is invalid, it equals
pdfWriter->setResolution(0);
Anyway after calling that,
pdfWriter->resolution()
tells me the current resolution is 1200.
For 1200dpi, to draw at the same position as 96dpi, every value should * 12.5.
So 500,500 is the same position as 40,40 on 96dpi.pdfWriter->setResolution(96); pdfPainter->drawText(QPoint(40, 40), "test");
pdfWriter->setResolution(1200); pdfPainter->drawText(QPoint(500, 500), "test");
-