首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QTableView打印

QTableView打印
EN

Stack Overflow用户
提问于 2010-06-30 14:57:51
回答 3查看 11K关注 0票数 4

我是QT新手,我试着从QTableView打印出来

我该怎么做呢?

非常感谢

EN

回答 3

Stack Overflow用户

发布于 2010-11-03 00:13:08

下面是第一个答案的一个变体,它去掉了中间文件。

代码语言:javascript
复制
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;
票数 11
EN

Stack Overflow用户

发布于 2010-06-30 15:28:45

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

1)。首先,我已经将表格数据保存到HTML页面文件:

代码语言:javascript
复制
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类打印文档:

代码语言:javascript
复制
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

}
票数 1
EN

Stack Overflow用户

发布于 2010-06-30 19:58:06

这个怎么样?

遍历您的QTableView模型,假设是QStandardItemModel。获取QStandardItemModel中可用项目的各个文本。

现在使用QTextCursor,将从模型获得的文本插入到QTextDocument中。您可以利用给定here的示例在QTextDocument中插入文本。

完成插入QTextDocument后,可以通过打印QTextDocument中的可用内容

代码语言:javascript
复制
void QTextDocument::print ( QPrinter * printer ) const

您必须确保能够遍历每个项,以便能够从模型获取所有项文本。

希望能有所帮助..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3147030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档