PDA

View Full Version : Issues with QTextDocument and QPrintPreviewDialog



H Aßdøµ
17th January 2015, 01:58
Hello,
I have strange problems seems I don't know even from where I should start. Based on this answer (http://stackoverflow.com/questions/3147030/qtableview-printing), and after modification, looks like QPrintPreviewDialog prints only one page from the report, even it should print at least 3 pages, as my model have over 250 row.
The second problem, after I print the page, it printed in very small size, even when it appears on QPrintPreviewDialog as it feat the page width.


void StudentNotes::printStudentsList()
{
QPrinter *printer = new QPrinter(QPrinter::PrinterResolution);
printer->setFullPage(true);
printer->setResolution( 90 );
printer->setPaperSize(QPrinter::A4);
printer->setOrientation(QPrinter::Landscape);
printer->setPageMargins (15,15,15,15, QPrinter::Millimeter);

QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(setupStudentsListPrint(QPrinter *)));
dlg->exec();
}

void StudentNotes::setupStudentsListPrint(QPrinter *printer)
{
QPainter painter;
painter.begin(printer);
QString strStream;
QTextStream out(&strStream);

const int rowCount = ui->listStudentsTable->model()->rowCount();
const int columnCount = ui->listStudentsTable->model()->columnCount();
out << "<html dir=\"rtl\">\n"
"<head>\n"
"<meta Content=\"text/html; charset=utf-8\">\n"
<< QString("<title>%1</title>\n").arg("Print test")
<< "<style> "
<< " html, body { width: 100%; padding: 0; margin: 0; font-size: 16px; }"
<< " table { page-break-inside:avoid }"
<< " tr { page-break-inside:avoid; page-break-after:auto }"
<< " thead { display:table-header-group }"
<< " tfoot { display:table-footer-group }"
<< "</style>"
<< "</head>\n"
"<body bgcolor=#ffffff link=#5000A0>\n"
"<table border=1 cellspacing=0 cellpadding=2>\n";

// headers
out << "<thead><tr bgcolor=#f0f0f0>";
for (int column = 0; column < columnCount; column++)
if (!ui->listStudentsTable->isColumnHidden(column))
out << QString("<th>%1</th>").arg(ui->listStudentsTable->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr></thead>\n";

// data table
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
if (!ui->listStudentsTable->isColumnHidden(column)) {
QString data = ui->listStudentsTable->model()->data(ui->listStudentsTable->model()->index(row, column)).toString().simplified();
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));

}
}
out << "</tr>\n";
}
out << "</table>\n"
"</body>\n"
"</html>\n";

// Just for debugging purposes
QFile file("htmlFileName.html");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
//MSG(QString("Can`t create file %1").arg(htmlFileName));
}

file.write(strStream.toLatin1());
file.close();
QSizeF paperSize;
paperSize.setWidth(printer->width());
paperSize.setHeight(printer->height());
QTextDocument *document = new QTextDocument();
QTextOption options;
options.setTextDirection(Qt::RightToLeft);
document->setDefaultTextOption(options);
document->setPageSize(paperSize);
document->setHtml(strStream);
document->drawContents(&painter);
painter.end();
}

ChrisW67
17th January 2015, 06:37
You only ever paint on the first page, i.e. you never call newPage() on the printer. You get whatever will fit on the current page and the rest will be chopped.

If you have a multi-page document in a QTextDocument then you should use QTextDocument::print() to get free pagination.

H Aßdøµ
17th January 2015, 12:53
Replacing QPainter with this line shows all pages to send to the printer:

document->print(printer);
Now, if I print directly from QPrintPreviewDialog to the printer, the printed text is too small, and by too small I mean as micro text. But when I printed as PDF file then print the pdf file the text printed showed as normal size, what should I do now?

H Aßdøµ
17th January 2015, 22:24
The too small text caused by calling QTextDocument::setPageSize before the document set any html, so swapping this two lines fixes the issue of tiny font text.

document->setHtml(strStream);
document->setPageSize(paperSize);
My last problem with printing(I hope so) is my report is right to left(Arabic language), but QPrintPreviewDialog shows it as left to right, even I set dir attribute to rtl in html tag.

ChrisW67
18th January 2015, 07:16
Take a look at the Supported HTML Subset for QTextDocument to see why setting the dir attribute on the html element is not working. Trying wrapping the content of the body element in a div element.

H Aßdøµ
14th February 2015, 23:04
That is bad. Even I after switched to QWebView, it has issues when it comes to show images.