我有一个接收模型数据并将其打印为 html 表的插槽:
void StudentNotes::setupStudentsListPrint(QPrinter *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 { direction: rtl; }"
<< " table { margin: 10px; page-break-after:auto; width: 100% }"
<< " tr { page-break-inside:avoid; page-break-after:auto }"
<< " td { font-family: \"Times New Roman\"; font-size: 16px; text-align: center; 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><div dir=\"rtl\">\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";
QString colsSpan = "colspan=1";
// data table
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
if ((row == rowCount -1) || (row == rowCount -2) || (row == rowCount -3))
colsSpan = "colspan=3";
//qDebug() << "dsdsdsds";
else
colsSpan = "colspan=1";
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();
if (column == 0)
out << QString("<td " + colsSpan + " bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
else
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
}
out << "</tr>\n";
}
out << "</table></div>\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.toUtf8());
file.close();
/*
* Prepare QTextDocument
*/
QSizeF paperSize;
paperSize.setWidth(printer->width());
paperSize.setHeight(printer->height());
QTextDocument *document = new QTextDocument();
QTextOption options;
options.setTextDirection(Qt::RightToLeft);
document->setDefaultTextOption(options);
document->setHtml(strStream);
document->setPageSize(paperSize);
document->adjustSize();
document->print(printer);
}
除了页面方向之外,一切都很好,如果我打印或显示它QPrintPreviewDialog
是从左到右出现的,但是当我htmlFileName.html
在浏览器中打开时,我会看到从右到左的方向。那么为什么QPrintPreviewDialog
没有看到它应有的样子呢?