How to access QTextDocument pages

旧巷老猫 提交于 2020-01-13 05:45:11

问题


I am trying to build an application that would display rich text documents in paginated fashion - more or less like MS Word does. For now, I would like to have only one page displayed at a time.

From this question I have learned that I need to represent the document by QTextDocument. While the author of the question focuses more on the view representation, I would like to learn about accessing document data in paginated fashion. I can see that the class has methods such as setPageSize() and pageCount(). Calling setPageSize() seems to update pageCount() accordingly.

The problem is I do not know how to access the contents of a chosen page to have it displayed in QtextEdit.

(I am accessing Qt from Python by means of PySide library)


回答1:


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);
}


来源:https://stackoverflow.com/questions/15874129/how-to-access-qtextdocument-pages

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