[SOLVED] Page number in QTextDocument for envelopes
I'm writing an application that prints addresses directly onto envelopes. I'm using QTextDocument and the problem is that its method print() adds the page number, which is incorrect in envelopes.
From the docs of the class:
bq. If the document is not paginated, like for example a document used in a QTextEdit, then a temporary copy of the document is created and the copy is broken into multiple pages according to the size of the QPrinter's paperRect(). By default a 2 cm margin is set around the document contents. In addition the current page number is printed at the bottom of each page.
How can I avoid this addition of the page number (which BTW I think should be optional)?
Some code, in case you need it:
QString addressText = textEdit->document()->toPlainText();
envelopeDocument = new QTextDocument(this);
QFont font("Trebuchet MS");
printer.setPaperSize(QSizeF(227,324), QPrinter::Millimeter); printer.setPageMargins(170,120,30,40,QPrinter::Millimeter); break; } QPrintPreviewDialog preview (&printer,this); preview.setWindowFlags(Qt::Window); connect(&preview, SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); preview.exec();
void MainWindow::printPreview(QPrinter *p)
hey u can try the following code, it might help you...
@ QPrinter printer(QPrinter::ScreenResolution);
printer.setOutputFileName( fileName );
// printer.setPageMargins(0.925, 0.8, 0.5, 0.8, QPrinter::Inch);
QSizeF paperSize; paperSize.setWidth(printer.width()); paperSize.setHeight(printer.height()); document->setHtml(html); document->setPageSize(paperSize); // the document needs a valid PageSize document->print(&printer);@
When you refer the source code of print(), then you will recognize that the < QPointF pageNumberPos; > is only defined when there is no valid QTextDocument.pageSize().
In printPage() the page number will be just printed, if pageNumberPos is not null.
So just set a valid QTextDocumtent.pageSize() and you have no page numbers on your printed document.
I checked the source code and it seems there's no if() statement to check if you want to have the page number printed out or not. Correct me if I'm wrong, but I believe everything is actually printed by this function:
@static void printPage(int index, QPainter *painter, const QTextDocument doc, const QRectF &body, const QPointF &pageNumberPos)@
It's being internally used by the print() method of QTextDocument. Maybe it would be possible to used that one directly and if you won't specify the pageNumberPos then it won't be printed out:
printing page number code
@d2uriel: Hi! Could you please tell me in which class did you find that function? I checked QTextDocument and QPrinter source code and didn't find it.
The thing is that that function should be public in order to be usable by other classes...
@David_Gil: I found it under Qt installation folder and then: src/gui/text/qtextdocument.cpp
hey did try my code snippet...
did it work 4 u..
@d2uriel: Sorry, I just found it. The function printPage can't be used directly, it isn't public.
@shoyeb: I'm going to do it right now :-)
@shoyeb: It worked!
Thank you very much (and happy hacking :-)