问题
I have createated a QTextDocument
with a table in it. Now I'm trying to render it into PDF format using QPdfWriter
(Qt 5.2.1). This is how I do it:
QPdfWriter pdfWriter(output);
QPainter painter(&pdfWriter);
doc->drawContents(&painter);
It works, but the problem is that the table in PDF is really, really tiny. What can I do to scale it up? I mean to scale up the whole document, not just this table, because I plan to add more contents to the document.
回答1:
With current Qt (>= 5.3) you just need to call: QPdfWriter::setResolution(int dpi)
回答2:
You can use QPdfWriter::setPageSizeMM()
or QPdfWriter::setPageSize()
to set size of a page. To test this idea you can just add pdfWriter.setPageSize(QPagedPaintDevice::A0);
in your code.
回答3:
The answer is to use QPainter::scale()
, so in my case:
QPdfWriter pdfWriter(output);
QPainter painter(&pdfWriter);
painter.scale(20.0, 20.0);
doc->drawContents(&painter);
This causes painter to paint everything 20 times bigger.
I still don't know why QPdfWriter
paints everything so tiny, but the problem can be solved as above.
回答4:
I've just been caught out by this too. I figured out what was going on when I called the widthMM()
and width()
functions of QPdfWriter
. The widthMM was about 200 which is right for an A4/Letter page (a sensible default), but the width
was returned as about 9600. I called logicalDpiX()
and that returned 1200.
So what this indicates is that the logical unit of the QPdfWriter
is the 'dot', where there are by default 1200 dots per inch. You therefore need to scale between your own logical units and 'dots'. For instance if your logical unit is a Point then you need to do something like this:
QPdfWriter writer(filename);
int logicalDPIX=writer.logicalDpiX();
const int PointsPerInch=72;
QPainter painter;
painter.begin(&writer)
QTransform t;
float scaling=(float)logicalDPIX/PointsPerInch; // 16.6
t.scale(scaling,scaling);
// do drawing with painter
painter.end()
painter.setTransform(t);
来源:https://stackoverflow.com/questions/24356736/qtextdocument-qpdfwriter-how-to-scale-output