Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Page number in QTextDocument for envelopes
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Page number in QTextDocument for envelopes

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 9.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David_Gil
    wrote on last edited by
    #1

    Hi,

    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:
    @
    void MainWindow::print()
    {
    QString addressText = textEdit->document()->toPlainText();
    envelopeDocument = new QTextDocument(this);
    printer.setResolution(QPrinter::HighResolution);
    printer.setPrinterName("OKI B6200(PCL6)");
    printer.setOrientation(QPrinter::Landscape);
    QFont font("Trebuchet MS");
    switch (envelopeComboBox->currentIndex()){
    case 0:
    font.setPointSize(12);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);
    printer.setPaperSize(QSizeF(114,225),QPrinter::Millimeter);
    printer.setPageMargins(120,60,20,15,QPrinter::Millimeter);
    break;
    case 1:
    font.setPointSize(14);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);
    printer.setPaperSize(QSizeF(184,262),QPrinter::Millimeter);
    printer.setPageMargins(140,100,20,20,QPrinter::Millimeter);
    break;
    case 2:
    font.setPointSize(16);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);

        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)
    {
    envelopeDocument->print(p);
    }@

    Thank you!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shoyeb
      wrote on last edited by
      #2

      hey u can try the following code, it might help you...

      @ QPrinter printer(QPrinter::ScreenResolution);
      printer.setPaperSize(QPrinter::A4);
      printer.setOutputFormat(QPrinter::PdfFormat);
      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.

      There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d2uriel
        wrote on last edited by
        #3

        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:
        @if (!pageNumberPos.isNull())
        /

        printing page number code
        */@

        "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

        1 Reply Last reply
        0
        • D Offline
          D Offline
          David_Gil
          wrote on last edited by
          #4

          @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...

          Regards :-)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d2uriel
            wrote on last edited by
            #5

            @David_Gil: I found it under Qt installation folder and then: src/gui/text/qtextdocument.cpp

            "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shoyeb
              wrote on last edited by
              #6

              hey did try my code snippet...
              did it work 4 u..

              There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                David_Gil
                wrote on last edited by
                #7

                @d2uriel: Sorry, I just found it. The function printPage can't be used directly, it isn't public.

                Regards

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  David_Gil
                  wrote on last edited by
                  #8

                  @shoyeb: I'm going to do it right now :-)

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    David_Gil
                    wrote on last edited by
                    #9

                    @shoyeb: It worked!

                    Thank you very much (and happy hacking :-)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved