Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pagination of a QTextDocument



  • Hello I have a QTextDocument which is printed as a multiple page document when I print it to a pdf file :

    @QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    printer.setPaperSize(QPrinter::A4);
    doc->print(&printer);@

    doc is my QTextDocument and when I want to display it in a QTextEdit it shows it in a continuous fashion, not in seperate pages.
    @
    QTextEdit* displayArea = new QTextEdit;
    displayArea->setDocument(document);@

    What should I do, if I want my QTextDocument to be shown in different QTextEdits per page or are there any method for displaying each page of my QTextDocument seperately like in printed pdf?



  • Actually, it can be done with 2 ways :

    1. We can seperate documents by hand and then can give each page to one qtextedit for displaying, when printing we can print with painter :
      @ QPainter painter;
      painter.begin(&printer);
      docs[0]->drawContents(&painter);
      printer.newPage();
      docs[1]->drawContents(&painter);
      painter.end();@

    here doc[0] is document of first text edit.
    2. We can take all document and we can have a paginated view with QPrintPreviewDialog :
    printPreview will open the dialog :
    @void OurWidget::printPreview()
    {
    QPrinter printer(QPrinter::ScreenResolution);
    QPrintPreviewDialog preview(&printer, this);
    preview.setMinimumSize(1000,800);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(preview(QPrinter*)));
    preview.exec();
    }@
    and preview is paint function of print preview widget :
    @void OurWidget::preview(QPrinter *printer)
    {
    doc->print(printer);
    }@

    here doc is a full document.


Log in to reply