Export QTableView to PDF
-
@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 :) -