QTableView printing

后端 未结 3 955
轻奢々
轻奢々 2020-12-01 13:49

I am new on QT an I try to print out from QTableView

How can I do this?

Thank a lot

相关标签:
3条回答
  • 2020-12-01 14:06

    Here is a variation of the first answer that gets rid of the intermediate file.

    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;
    
    0 讨论(0)
  • 2020-12-01 14:12

    here is my solution of a problem. May be it's too complex... But here you could find more solutions!

    1). first I have saved table data to HTML page file:


    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). after I have saved html content to file, it was opened in html view window, where I could print the document with QTextBrowser class:


    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 讨论(0)
  • 2020-12-01 14:23

    How about this one?

    Traverse through the model of your QTableView let's say QStandardItemModel. Obtain the each and individual texts of the items available in the QStandardItemModel.

    Now using QTextCursor, insert the obtained texts from your model into QTextDocument. You can make use of the examples given here to insert text into QTextDocument.

    After the completion of inserting into QTextDocument, you can print the contents available in the QTextDocument through

    void QTextDocument::print ( QPrinter * printer ) const
    

    The thing you have to make sure is that you should be able to traverse through each items so that you can able obtain all the item text from the model.

    Hope it helps..

    0 讨论(0)
提交回复
热议问题