Export QTableView to PDF
-
@Gojir4
Thank you, this is very interesting. (I'm a Qt noob, so didn't know aboutQTextDocument
.)This approach is similar in principle to my #1, or https://stackoverflow.com/questions/3147030/qtableview-printing. However, instead of having to directly generate the HTML for my table myself, this is a "structured" document, which offers objects like
QTextTable
,QTextTableCell
etc. which I can use to construct the desired structure. I can then print from that and (hopefully!) get a layout somewhat similar to theQTableView
.I also note that there is even a http://doc.qt.io/qt-5/qtextdocument.html#toHtml method to get an HTML equivalent (which I presume will use
<TABLE>
etc.) Which is nice. If I want to, I could even doubtless poke that atQWebEnginePage
to use itsprintToPdf()
, which I already employ elsewhere :)So --- unless someone else wants to tell me about approach #2 instead --- I think I'll shortly give this a go and see how it comes out...!
-
Hi
I made a fast version of raw html output here
https://forum.qt.io/topic/52652/solved-pdf-print-in-multiple-pages/22
It fast becomes a bit messy if you want to use lots of formatting for good looks.Constructing a Text Document using its api should be much cleaner :)
-
@mrjj
Thanks. Yep, that was one of the posts I looked at (the stackoverflow one, I mean).Using the
QTextDocument
looks cleaner, and relieves me of producing the HTML, so I'll give that a go. If you're saying it won't look as good as the hand-crafted HTML, I'll think again when I get there.Meanwhile neither of you is suggesting #2:
I read, say, https://forum.qt.io/topic/30728/how-to-turn-a-qtableview-to-a-pdf
You write code to paint the data from the model underlying the table view to a QPrinter set to output PDF. How you access the data and format it is entirely up to you.
the "Paint" approach. Which is fine by me! Though now I'm curious as to how you actually do that, as I said I haven't gone near painting?
-
@JonB
Hi, im saying it will be easier to make it look better than handcrafted as
you can use higher level classes like
http://doc.qt.io/qt-5/qtexttableformat.html
and CharFormat etc and its easier to scale/resize
My main point is that constructing HTML was not super nice in terms of readability and
reuse of html parts/styling etc. Just my feeling though. If you are master at html you might produce cleaner html than my run at it :)The pure paint way would to create a drawTable function and something to draw the cell text/style and
set properties on QPainter for bold font etc. For a very plain table, its not very complex but
for varying cell widths and extra formatting, you suddenly have to have a small structure to keep that info and
it slowly becomes big(ger)
Also, you would have to keep a YPos for newPage handling and other small details.For full blown printing, something like
https://sourceforge.net/projects/qtrpt/
is also very useful :) -
@mrjj said in Export QTableView to PDF:
The pure paint way would to create a drawTable function and something to draw the cell text/style and
[...]I certainly would not want to do any styling, bolding, drawing at all! I thought the way the guy said that meant that you could somehow just tell
QTableView
to output to a QPrinter set to output PDF instead of to the screen, and it just handled all the drawing itself? -
@JonB They are several advantages, at my opinion, to use QTextDocument:
- You can preview/edit in a QTextEdit or QPlainTextEdit
- You can export content to Open Document Format (Open Office Writer), HTML, PDF and plaintext of course.
- You can customize everything (font, table, cells, etc..).
-
@JonB
Well you can use render() to make it draw it self to QPrinter
This sample paint to pixmap but idea is the same.
However, this is only nice if all rows are visible as it wont paint all of them. only how Widget looks on screen.
Since QPrinter have much higher DPI/pixels, you can scale the widget to use all space but any rows
not visible are not handled.
Im not aware of anything else in terms of directly printing the TableView.void MainWindow::PrintWidget(QWidget* widget) { QPixmap pix(widget->size()); QPainter painter(&pix); widget->render(&painter); painter.end(); QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName("test.pdf"); // will be in build folder painter.begin(&printer); double xscale = printer.pageRect().width() / double(pix.width()); double yscale = printer.pageRect().height() / double(pix.height()); double scale = qMin(xscale, yscale); painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2, printer.paperRect().y() + printer.pageRect().height() / 2); painter.scale(scale, scale); painter.translate(-widget->width() / 2, -widget->height() / 2); painter.drawPixmap(0, 0, pix); QTextDocument doc; doc.setHtml("htmlcontent"); doc.drawContents(&painter); painter.end(); }
-
@mrjj
Thanks. I think:Well you can use render() to make it draw it self to QPrinter
is what I was trying to find,
QTableView::render(&QPrinter)
. I understand your example too. Understand about "it wont paint all of them", have a look at https://stackoverflow.com/a/9784152/489865 for one guy's solution to that.I understand enough now to prefer to go down the
QTextDocument
route for my situation. I have a table of values here, I'm not tied to the physicalQTableView
visuals, and I'm already offering export to CSV file, so export to PDF via a structured document with a table is good. Plus I get text or HTML too if I want them :) -