PDF 查看器範例

渲染 PDF 文檔。

[Missing image pdfviewer.png]

PDF Viewer demonstrates how to use the QPdfDocument class to render PDF documents and the QPdfPageNavigation class to navigate them.

Qt Creator and the integrated Qt Designer were used to create the example UI and to connect it to the code. This affects the code, which might be somewhat different to what you would typically write by hand. For more information about using Qt Designer, see Qt Designer Manual and Qt Creator:創建 Qt Widget 基應用程序 .

運行範例

要運行範例從 Qt Creator ,打開 歡迎 模式,然後選擇範例從 範例 。更多信息,拜訪 構建和運行範例 .

創建主窗口

The MainWindow class inherits the QMainWindow 類:

類聲明匹配選擇器動作的公共和私有槽:

The actual layout of the main window is specified in a .ui file. The widgets and actions are available at runtime in the ui member variable.

The m_zoomSelector variable holds the zoom selector and the m_pageSelector holds the page selector. The m_document variable is an instance of the QPdfDocument class that contains the PDF document.

The actual setup of the different objects is done in the MainWindow constructor:

構造函數首先調用 setupUi() to construct the zoom and page selectors according to the UI file. We set the maximum width of the selectors.

We use the QPdfPageNavigation class to handle the navigation through a PDF document:

連接 zoomModeChanged and zoomFactor changed signals of the PDF view to the functions that reset the zoom selector:

We then load the PDF document to the viewer:

    ...
					

Finally, we connect the zoomFactorChanged signal to the function that sets the value of the zoom selector:

文件和歸屬

範例工程 @ code.qt.io

內容

  1. 運行範例

  2. 創建主窗口

  3. 文件和歸屬