将QTextDocument页面方向设置为从右到左

时间:2015-01-18 00:46:43

标签: html qt qtextdocument

我有一个插槽,可以获取模型数据并将其打印为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("&nbsp;"));
                else
                    out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
            }
        }
        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没有按原样看到它呢?

1 个答案:

答案 0 :(得分:0)

事实证明QTextDocument仅支持部分HTML标记和CSS 2.1规则。

Supported HTML Subset