PDA

View Full Version : Qt 5.11 Pagination



ad5xj
14th June 2018, 21:00
I have a simple application with a database of book titles and authors.
I have created a query that I can print or preview displaying the result set.
My problem is that I want to change the default footer of the report to one that I can format, something like "01/31/1900 Page 1 of 13".

I would like to know several things:

1) what signal indicates a page break is about to occur.
2) and / or how do I format a footer for the breaking page.
3) since this is a document in a QTextEdit document is the formating done in the editor or in one or more of the document formats/frames?

This is my first jump into document printing with Qt. I am missing something but I am not clear as to what.



void MainWindow::getAllTitles(QPrinter *printer)
{
d->db = QSqlDatabase::database("booksconn",true);
QDate date = QDate::currentDate();
QString strDate = date.toString("d MMM yyyy");

printer->setPaperSize(QPrinter::Letter);
// create a font appropriate to page size
QFont bodyFont;
QFont titleFont;
titleFont.setStyleHint(QFont::Courier);
titleFont.setPointSize(20);
bodyFont.setStyleHint(QFont::Helvetica);

ui->editor->clear();
QTextCursor acursor(ui->editor->textCursor());
acursor.movePosition(QTextCursor::Start);
QTextFrame *tpFrame = acursor.currentFrame();

QTextFrameFormat tFrameFormat;
QTextFrameFormat bFrameFormat;
bFrameFormat.setWidth(QTextLength(QTextLength::Per centageLength, 100));

QTextCharFormat textFormat;
QTextCharFormat boldFormat;
QTextCharFormat titleFormat;
textFormat.setFont(bodyFont);
titleFormat.setFont(titleFont);
titleFormat.setFontWeight(QFont::Bold);
boldFormat.setFontWeight(QFont::Bold);
QTextFrame *tFrame = acursor.currentFrame();
tFrame->setFrameFormat(tFrameFormat);
acursor.insertFrame(tFrameFormat);
acursor.setPosition(0);
QString hdrtxt = "<h1 style=\"text-align: center; font-family: \'";
hdrtxt += d->rtitleFont;
hdrtxt += "\'; font-size: 20pt; font-weight: bold;\">";
hdrtxt += "All Book Titles By Title";
hdrtxt += "</h1><br>";

acursor.insertHtml(hdrtxt);
QString dttxt = "<div style=\"text-align: center; font-family: \'";
dttxt += d->rtitleFont;
dttxt += "\' font-size: 11pt; font-weight: normal;\">";
dttxt += strDate;
dttxt += "</div>";

acursor.insertHtml(dttxt);
acursor.setPosition(tFrame->lastPosition());
acursor.endEditBlock();
acursor.setPosition(tpFrame->lastPosition());

bFrameFormat.setWidth(QTextLength(QTextLength::Per centageLength, 100));

QTextFrame *topFrame = acursor.currentFrame();
QTextFrameFormat topFrameFormat = topFrame->frameFormat();
QTextFrameFormat titleFrameFormat;
QTextFrameFormat bodyFrameFormat;

QTextFrame *titleFrame = acursor.currentFrame();
topFrameFormat.setPadding(10);
topFrame->setFrameFormat(topFrameFormat);

titleFrameFormat.setBorder(0);
titleFrame->setFrameFormat(titleFrameFormat);


QTextFrame *bodyFrame = acursor.currentFrame();
bodyFrame->setFrameFormat(bodyFrameFormat);

QSqlQuery qry(d->db);
QString strDML;
strDML = "SELECT DISTINCT ";
strDML += "Books.Title, Authors.LastName||', '||Authors.FirstName Author, ";
strDML += "MediaType.Media ";
strDML += "FROM ";
strDML += " Books, Authors, MediaType ";
strDML += "WHERE ";
strDML += " Books.AuthKey = Authors.ID AND ";
strDML += " Books.MediaKey = MediaType.ID ";
strDML += "ORDER BY Books.Title, Author ";

qry.exec(strDML);
qry.first();
qry.last();
qry.first();

QTextTableFormat tblFormat;
tblFormat.setColumns(5);
tblFormat.setCellPadding(1);
tblFormat.setCellSpacing(2);
tblFormat.setPageBreakPolicy(QTextFormat::PageBrea k_Auto);
tblFormat.setHeaderRowCount(1);
acursor.insertBlock();
acursor.beginEditBlock();
QTextTable *tableBooks = acursor.insertTable(1,5,tblFormat);
tableBooks->format().setPageBreakPolicy(QTextFormat::PageBreak _Auto);
acursor = tableBooks->cellAt(0, 0).firstCursorPosition();
QString coltxt;
QApplication::setOverrideCursor(Qt::WaitCursor);
QApplication::processEvents();
acursor = tableBooks->cellAt(0,0).firstCursorPosition();
coltxt = "<tr style=\"height: 15px; margin: 0; padding: 0;\">";
coltxt += "<th style=\"";
coltxt += "margin: 0; padding: 0; text-align: center; font-size: 14pt; font-family: '";
coltxt += d->rsectionFont;
coltxt += "'; border-bottom: 2px solid black;\">";
coltxt += "<u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
coltxt += "Book Title&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u></th>";
acursor.insertHtml(coltxt);
acursor = tableBooks->cellAt(0,1).firstCursorPosition();
coltxt = "<th style=\"";
coltxt += "margin: 0; padding: 0; text-align: center; font-size: 14pt; font-family: '";
coltxt += d->rsectionFont;
coltxt += "'; border-bottom: 2px solid black;\">&nbsp;</th>";
acursor.insertHtml(coltxt);
acursor = tableBooks->cellAt(0,2).firstCursorPosition();
coltxt = "<th style=\"";
coltxt += "margin: 0; padding: 0; text-align: center; font-size: 14pt; font-family: '";
coltxt += d->rsectionFont;
coltxt += "'; border-bottom: 2px solid black;\"><u>&nbsp;&nbsp;Author Name&nbsp;&nbsp;</u></th>";
acursor.insertHtml(coltxt);
acursor = tableBooks->cellAt(0,3).firstCursorPosition();
coltxt = "<th style=\"";
coltxt += "margin: 0; padding: 0; text-align: center; font-size: 14pt; font-family: '";
coltxt += d->rsectionFont;
coltxt += "'; border-bottom: 2px solid black;\"><u>&nbsp;&nbsp;Media&nbsp;&nbsp;</u></th></tr>";
acursor.insertHtml(coltxt);
int j = 1;
do
{
if ( qry.record().field("Title").value().toString().trimmed() == "" ) break;
tableBooks->insertRows(j,1);
acursor = tableBooks->cellAt(j, 0).firstCursorPosition();
acursor.insertText(qry.record().field("Title").value().toString().trimmed());
acursor = tableBooks->cellAt(j,1).firstCursorPosition();
acursor.insertText(" ");
acursor = tableBooks->cellAt(j,2).firstCursorPosition();
acursor.insertText(qry.record().field("Author").value().toString().trimmed());
acursor = tableBooks->cellAt(j,3).firstCursorPosition();
acursor.insertText(qry.record().field("Media").value().toString().trimmed());
++j;

} while ( qry.next() );
qry.finish();
acursor.setPosition(bodyFrame->lastPosition());
acursor.endEditBlock();
acursor.setPosition(topFrame->lastPosition());
acursor.endEditBlock();

ui->editor->print(printer);
}


Any help in this area would be a big asset.