-
Notifications
You must be signed in to change notification settings - Fork 0
/
printingservice.cpp
53 lines (41 loc) · 1.72 KB
/
printingservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "printingservice.h"
void PrintingService::printThis(QTableView *tableToPrint){
QString strStream;
QTextStream out(&strStream);
QString strTitle;
const int rowCount = tableToPrint->model()->rowCount();
const int columnCount = tableToPrint->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 = columnCount-1; column >= 0 ; column--)
if (!tableToPrint->isColumnHidden(column))
out << QString("<th>%1</th>").arg(tableToPrint->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr></thead>\n";
// data table
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
for (int column = columnCount-1; column >=0 ; column--) {
if (!tableToPrint->isColumnHidden(column)) {
QString data = tableToPrint->model()->data(tableToPrint->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";
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;
}