How to access QTextDocument pages

懵懂的女人 提交于 2019-12-04 16:33:30

The QTextEdit is a text editor and does not really have a concept of pages, instead it's focused around paragraphs.

You could create your own paginated view that would respect the page sizes, but Qt already provide one for you. Have a look at the QPrintPreviewWidget or QPrintPreviewDialog. They are easy to use with QTextEdit.

Subclass QTextEdit and implement the following functions (the preview function is a Qt slot). The printPreview function will show a paginated view in a dialog with the contents of your QTextEdit.

void MyTextEdit::printPreview(QPrinter *printer)
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(preview(QPrinter*)));
    preview.exec();
}

void MyTextEdit::preview(QPrinter *printer)
{
     print(printer);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!