Solved make a beautiful pdf with qt
-
If memory serves well, the "C++ GUI Programming with Qt 4" book has a chapter dedicated to that with examples. Even if it's Qt 4, the concepts still applies here.
-
to take the whole qtablewidget with a lot of rows and columns, just go to the .ui and change the :
minimum sizethen we use this piece of code to insert the tab in a pdf :
QPixmap pix(QSize(1800,1000)); //with the new size of minimum size QPainter painter(&pix); ui->tab_indic->render(&painter); painter.end(); QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Portrait); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setPageMargins(15,15,15,15,QPrinter::Millimeter); printer.setOutputFileName("test.pdf"); // will be in build folder painter.begin(&printer); painter.drawText(0,printer.pageRect().y()*1,QString::fromUtf8("Ligne 0")); painter.save(); // save the parameters of painter for a normal use to write texte qDebug() <<"before translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size(); double xscale = printer.pageRect().width() / double(pix.width()); double yscale = printer.pageRect().height() / double(pix.height()); double scale = qMin(xscale, yscale); qDebug() <<"scale = "<< scale; /////////// Translate new parameters to the painter for draw the qtablewidget ///////////////// painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,printer.paperRect().y() + printer.pageRect().height() / 2); painter.scale(scale, scale); painter.translate(-ui->tab_indic->width() / 2, -ui->tab_indic->height() / 2); painter.drawPixmap(0, 0, pix); qDebug() <<"post translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width() <<printer.pageRect().height()<<ui->tab_indic->size() <<printer.paperRect().x() + printer.pageRect().width() / 2 <<printer.paperRect().y() + printer.pageRect().height() / 2; painter.restore();// restore the parameters save latest qDebug() <<"post restore"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size(); QFont police("Tekton Pro",10); QFontMetrics tailleMotPx = painter.fontMetrics(); painter.setPen(Qt::black); painter.drawText((printer.paperRect().x())/2,0,"titre"); painter.drawText(0, printer.pageRect().y()*2,QString::fromUtf8("Ligne 1")); printer.newPage();
I still have some questions to do a mad pdf but for now it's cool.
-
Hi
Can i ask what a "mad pdf" is ? -
it's a good question,@mrjj
well I'd say it's a mad pdf when it's visually beautiful but not only when I've managed to make a code that automate pdf creation by including all the widgets I need in a fast and ergonomic way without any placement hassle, no problem to cut a table when the table is bigger than the page and it doesn't display on another page etc etc etc
it's not exactly crazy for programming champions like you jajaja! -
-
limereport it looks really nice, I didn't know it at all ! then you have to see if it's easy to use.
For the moment the report I want to create contains only:- 1 table
- 2 graphs
- a dozen text boxes in qlineEdit or qtextEdit
it's not a big deal. but the report could be useful one day thanks to me :)
and I have one more question:
Can you make line breaks appear in qtextEdit with a basic painter.drawText? -
@Albator said in make a beautiful pdf with qt:
Can you make line breaks appear in qtextEdit with a basic painter.drawText?
drawText can wordwrap if you use the overload that takes a rect.
using LimeReport might be a good option as not to hand program all of it.
Its very powerfull and once you learn it good, you can make almost anything. -
@mrjj said in make a beautiful pdf with qt:
Its very powerfull and once you learn it good, you can make almost anything.
Hi,
Absolutely true...Dive into LimeReport... :)
Regards,
Mucip:) -
-
I've been watching limereport and I'm thinking that at first glance it's not bad because you can find all our widgets directly in the database and all you have to do is drag and drop them to get them in a report.
But it doesn't do it automatically? we have to place them manually, at first glance anyway.
-
@Albator
Well that is very normal with report generators as its very hard to guess
what to put in the report for the app.
So often that is the creative process.