here is my solution to the problem. Maybe this is too complicated ... But here you could find more solutions!
1). I first saved the table data in an 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(" ")); } } out << "</tr>\n"; } out << "</table>\n" "</body>\n" "</html>\n"; file.close(); } return true; }
2). after I saved the html contents to a file, it was opened in the html viewer, where I could print the document using the 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 }
source share