QTableView Printing

I am new to QT and trying to print it from QTableView

How can i do this?

Many thanks

+4
source share
3 answers

Here is a variant 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; 
+8
source

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("&nbsp;")); } } 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 } 
+1
source

How about this?

Go through the model of your QTableView , say QStandardItemModel . Get each and separate texts of the elements available in QStandardItemModel .

Now, using QTextCursor , paste the resulting texts from your model into the QTextDocument . You can use the examples below to insert text into a QTextDocument .

After you finish entering the QTextDocument you can print the content available in the QTextDocument through

 void QTextDocument::print ( QPrinter * printer ) const 

The thing you need to make sure is that you have to go through each element so that you can get all the element text from the model .

Hope this helps.

0
source

Source: https://habr.com/ru/post/1314294/


All Articles