QTableView 打印

4
我该如何从 QTableView 中打印输出?

请参见:https://stackoverflow.com/questions/74964248/qt-library-to-print-qtableview-qtablewidget-and-export-them-to-pdf - Ghon
3个回答

11

这是第一个答案的一种变体,它去除了中间文件。

QString strStream;
QTextStream out(&strStream);

const int rowCount = pPublic->tableView->model()->rowCount();
const int columnCount = pPublic->tableView->model()->columnCount();

out <<  "<html>\n"
    "<head>\n"
    "<meta Content=\"Text/html; charset=Windows-1251\">\n"
    <<  QString("<title>%1</title>\n").arg(strTitle)
    <<  "</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 (!pPublic->tableView->isColumnHidden(column))
        out << QString("<th>%1</th>").arg(pPublic->tableView->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 (!pPublic->tableView->isColumnHidden(column)) {
            QString data = pPublic->tableView->model()->data(pPublic->tableView->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";

QTextDocument *document = new QTextDocument();
document->setHtml(strStream);

QPrinter printer;

QPrintDialog *dialog = new QPrintDialog(&printer, NULL);
if (dialog->exec() == QDialog::Accepted) {
    document->print(&printer);
}

delete document;

谢谢,它起作用了。不过,在打印命令之前让用户查看打印版本表格会更好。 - McLan

1

这是我的一个编程问题的解决方案。也许它太复杂了,但是在这里你可以找到更多的解决方案!

1)首先我将表格数据保存到HTML页面文件中:


bool CRefViewerDlg::createHtmlTableFromModel() {

    // make a html-dump of table view
    if (tableView) {

        const QString htmlFileName = QString("%1/%2").arg(qApp->applicationDirPath()).arg("myTable.html");

        QFile file(htmlFileName);

        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            MSG(QString("Can`t create file %1").arg(htmlFileName));
            return false;
        }

        QTextStream out(&file);

        const xbLong rowCount = tableView->model()->rowCount();
        const xbLong columnCount = tableView->model()->columnCount();

        out <<  "<html>\n"
                "<head>\n"
                "<meta Content=\"Text/html; charset=Windows-1251\">\n"
            <<  QString("<title>%1</title>\n").arg(refTitleName)
            <<  "</head>\n"
                "<body bgcolor=#ffffff link=#5000A0>\n"
                "<table border=1 cellspacing=0 cellpadding=2>\n";

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

        // data table
        for (xbLong row = 0; row < rowCount; row++) {
            out << "<tr>";
            for (xbLong column = 0; column < columnCount; column++) {
                if (!tableView->isColumnHidden(column)) {
                    QString data = tableView->model()->data(tableView->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";

        file.close();
    }

    return true;
}

2). 在我将HTML内容保存到文件后,它会在HTML视图窗口中打开,在那里我可以使用QTextBrowser类打印文档:


void CLiveListDlg::on_printPageToolButton_clicked() {

#ifndef QT_NO_PRINTER
    QTextBrowser *editor = static_cast<QTextBrowser* >(textBrowser);
    QPrinter printer;

    QPrintDialog *dialog = new QPrintDialog(&printer, this);
    dialog->setWindowTitle(tr("Print Document"));
    if (editor->textCursor().hasSelection())
        dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
    if (dialog->exec() != QDialog::Accepted)
        return;

    editor->print(&printer);
#endif

}

0

这样怎么样?

遍历您的QTableView模型,例如QStandardItemModel。获取QStandardItemModel中每个项目的单个文本。

现在使用QTextCursor,将从您的模型获取的文本插入QTextDocument。您可以使用此处提供的示例将文本插入QTextDocument

完成向QTextDocument插入后,您可以通过以下方式打印QTextDocument中可用的内容

void QTextDocument::print ( QPrinter * printer ) const

你需要确保的是能够遍历每个项,这样才能从模型中获取所有项的文本。

希望对你有所帮助。


这个会打印表结构吗? - Adri C.S.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接