PRINTING A DISPLAYED TABLE FROM DATABASE
-
HELLO FRIENDS I need your help please, i have an application which is able to display tables from the data base using the tableview. Now want to be able to print these tables.Please any one with examples on how to print tables from data base or any code which can help me, i will really appreciate your help
@GREYON said in PRINTING A DISPLAYED TABLE FROM DATABASE:
Now want to be able to print these tables
You may want to take a look at QtRPT report generator. Spoiler alert: I haven't used it myself.
-
Hi
The code prints the actual widget so it cannot be used if many items as then not all are printed.
You can use the way Gojir4 shows using a QTextDocument or using html as i did here
https://forum.qt.io/topic/52652/solved-pdf-print-in-multiple-pages/21In any case you have to control the printing yourself and if you have many items so that cannot be on one page then you also need to handle new pages etc.
-
@GREYON said in PRINTING A DISPLAYED TABLE FROM DATABASE:
Now want to be able to print these tables
You may want to take a look at QtRPT report generator. Spoiler alert: I haven't used it myself.
-
Just to add:
PUPILS_PAID_SCH_FEE_BY_GRADE
is a horrible name for any c++ class. Name your widgets after what they are, not what content they display, since the content could change. In addition it's way too long, as you can see from how yourcpp
file looks like :)PupilFeeDialog
for example, would be much shorter and easier to handle. -
Just to add:
PUPILS_PAID_SCH_FEE_BY_GRADE
is a horrible name for any c++ class. Name your widgets after what they are, not what content they display, since the content could change. In addition it's way too long, as you can see from how yourcpp
file looks like :)PupilFeeDialog
for example, would be much shorter and easier to handle. -
@GREYON said in PRINTING A DISPLAYED TABLE FROM DATABASE:
Now want to be able to print these tables
You may want to take a look at QtRPT report generator. Spoiler alert: I haven't used it myself.
-
Hi
The code prints the actual widget so it cannot be used if many items as then not all are printed.
You can use the way Gojir4 shows using a QTextDocument or using html as i did here
https://forum.qt.io/topic/52652/solved-pdf-print-in-multiple-pages/21In any case you have to control the printing yourself and if you have many items so that cannot be on one page then you also need to handle new pages etc.
-
Im not sure what you see ?
it should all be there. just need to scroll more :)void PrintTable( QPrinter* printer, QSqlQuery& Query )
is in any case.bool createConnection() {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel);
return false;
}
QSqlQuery query;
qDebug() << "table:" << query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20), num int )");
query.exec("insert into person values(101, 'Dennis', 'Young','1')");
query.exec("insert into person values(102, 'Christine', 'Holand','2')");
query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
return true;
}// credits to. (i adabted from his) https://stackoverflow.com/questions/3147030/qtableview-printing/4079676#4079676
void PrintTable( QPrinter* printer, QSqlQuery& Query ) {
QString strStream;
QTextStream out(&strStream);const int rowCount = Query.size();
const int columnCount = Query.record().count();out << "<html>\n"
"<head>\n"
"<meta Content="Text/html; charset=Windows-1251">\n"
<< QString("<title>%1</title>\n").arg("TITLE OF TABLE")
<< "</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++)
out << QString("<th>%1</th>").arg(Query.record().fieldName(column));
out << "</tr></thead>\n";while (Query.next()) {
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
QString data = Query.value(column).toString();
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
out << "</tr>\n";
}out << "</table>\n"
"</body>\n"
"</html>\n";QTextDocument document;
document.setHtml(strStream);
document.print(printer);}
void print() {
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Portrait);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
// printer.setOutputFileName("e:/file.pdf"); // just for me testing
QPrintDialog dlg(&printer, 0);
if(dlg.exec() == QDialog::Accepted) {
QSqlQuery query;
query.exec("SELECT * from person");
PrintTable(&printer, query);
}
}