QTableView printing

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;

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

}