void StudentNotes::printStudentsList()
{
printer->setFullPage(true);
printer->setResolution( 90 );
printer
->setOrientation
(QPrinter::Landscape);
printer
->setPageMargins
(15,
15,
15,
15,
QPrinter::Millimeter);
QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
connect(dlg,
SIGNAL(paintRequested
(QPrinter *)),
this,
SLOT(setupStudentsListPrint
(QPrinter *)));
dlg->exec();
}
void StudentNotes
::setupStudentsListPrint(QPrinter *printer
) {
painter.begin(printer);
const int rowCount = ui->listStudentsTable->model()->rowCount();
const int columnCount = ui->listStudentsTable->model()->columnCount();
out << "<html dir=\"rtl\">\n"
"<head>\n"
"<meta Content=\"text/html; charset=utf-8\">\n"
<<
QString("<title>%1</title>\n").
arg("Print test") << "<style> "
<< " html, body { width: 100%; padding: 0; margin: 0; font-size: 16px; }"
<< " table { page-break-inside:avoid }"
<< " tr { page-break-inside:avoid; page-break-after:auto }"
<< " thead { display:table-header-group }"
<< " tfoot { display:table-footer-group }"
<< "</style>"
<< "</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 (!ui->listStudentsTable->isColumnHidden(column))
out <<
QString("<th>%1</th>").
arg(ui
->listStudentsTable
->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 (!ui->listStudentsTable->isColumnHidden(column)) {
QString data
= ui
->listStudentsTable
->model
()->data
(ui
->listStudentsTable
->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";
// Just for debugging purposes
QFile file("htmlFileName.html");
//MSG(QString("Can`t create file %1").arg(htmlFileName));
}
file.write(strStream.toLatin1());
file.close();
paperSize.setWidth(printer->width());
paperSize.setHeight(printer->height());
options.setTextDirection(Qt::RightToLeft);
document->setDefaultTextOption(options);
document->setPageSize(paperSize);
document->setHtml(strStream);
document->drawContents(&painter);
painter.end();
}
void StudentNotes::printStudentsList()
{
QPrinter *printer = new QPrinter(QPrinter::PrinterResolution);
printer->setFullPage(true);
printer->setResolution( 90 );
printer->setPaperSize(QPrinter::A4);
printer->setOrientation(QPrinter::Landscape);
printer->setPageMargins (15,15,15,15, QPrinter::Millimeter);
QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(setupStudentsListPrint(QPrinter *)));
dlg->exec();
}
void StudentNotes::setupStudentsListPrint(QPrinter *printer)
{
QPainter painter;
painter.begin(printer);
QString strStream;
QTextStream out(&strStream);
const int rowCount = ui->listStudentsTable->model()->rowCount();
const int columnCount = ui->listStudentsTable->model()->columnCount();
out << "<html dir=\"rtl\">\n"
"<head>\n"
"<meta Content=\"text/html; charset=utf-8\">\n"
<< QString("<title>%1</title>\n").arg("Print test")
<< "<style> "
<< " html, body { width: 100%; padding: 0; margin: 0; font-size: 16px; }"
<< " table { page-break-inside:avoid }"
<< " tr { page-break-inside:avoid; page-break-after:auto }"
<< " thead { display:table-header-group }"
<< " tfoot { display:table-footer-group }"
<< "</style>"
<< "</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 (!ui->listStudentsTable->isColumnHidden(column))
out << QString("<th>%1</th>").arg(ui->listStudentsTable->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 (!ui->listStudentsTable->isColumnHidden(column)) {
QString data = ui->listStudentsTable->model()->data(ui->listStudentsTable->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";
// Just for debugging purposes
QFile file("htmlFileName.html");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
//MSG(QString("Can`t create file %1").arg(htmlFileName));
}
file.write(strStream.toLatin1());
file.close();
QSizeF paperSize;
paperSize.setWidth(printer->width());
paperSize.setHeight(printer->height());
QTextDocument *document = new QTextDocument();
QTextOption options;
options.setTextDirection(Qt::RightToLeft);
document->setDefaultTextOption(options);
document->setPageSize(paperSize);
document->setHtml(strStream);
document->drawContents(&painter);
painter.end();
}
To copy to clipboard, switch view to plain text mode
Bookmarks