Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c++ - QTableView printing

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

How can I do this?

Thank a lot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>
"
    "<head>
"
    "<meta Content="Text/html; charset=Windows-1251">
"
    <<  QString("<title>%1</title>
").arg(strTitle)
    <<  "</head>
"
    "<body bgcolor=#ffffff link=#5000A0>
"
    "<table border=1 cellspacing=0 cellpadding=2>
";

// 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>
";

// 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>
";
}
out <<  "</table>
"
    "</body>
"
    "</html>
";

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...