渲染 PDF 文档。
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 手册 and Qt Creator:创建 Qt Widget 基应用程序 .
要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .
The MainWindow class inherits the QMainWindow 类:
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow();
类声明匹配选择器动作的公共和私有槽:
public slots: void open(const QUrl &docLocation); private slots: void bookmarkSelected(const QModelIndex &index); // action handlers void on_actionOpen_triggered(); void on_actionQuit_triggered(); void on_actionAbout_triggered(); void on_actionAbout_Qt_triggered(); void on_actionZoom_In_triggered(); void on_actionZoom_Out_triggered(); void on_actionPrevious_Page_triggered(); void on_actionNext_Page_triggered(); void on_actionContinuous_triggered();
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.
private: Ui::MainWindow *ui;
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.
ZoomSelector *m_zoomSelector; PageSelector *m_pageSelector; QPdfDocument *m_document; };
The actual setup of the different objects is done in the MainWindow constructor:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_zoomSelector(new ZoomSelector(this)) , m_pageSelector(new PageSelector(this)) , m_document(new QPdfDocument(this)) {
构造函数首先调用
setupUi()
to construct the zoom and page selectors according to the UI file. We set the maximum width of the selectors.
ui->setupUi(this); m_zoomSelector->setMaximumWidth(150); ui->mainToolBar->insertWidget(ui->actionZoom_In, m_zoomSelector); m_pageSelector->setMaximumWidth(150); ui->mainToolBar->addWidget(m_pageSelector);
We use the QPdfPageNavigation class to handle the navigation through a PDF document:
m_pageSelector->setPageNavigation(ui->pdfView->pageNavigation());
连接
zoomModeChanged
and
zoomFactor
changed signals of the PDF view to the functions that reset the zoom selector:
connect(m_zoomSelector, &ZoomSelector::zoomModeChanged, ui->pdfView, &QPdfView::setZoomMode); connect(m_zoomSelector, &ZoomSelector::zoomFactorChanged, ui->pdfView, &QPdfView::setZoomFactor); m_zoomSelector->reset();
We then load the PDF document to the viewer:
... ui->pdfView->setDocument(m_document);
Finally, we connect the
zoomFactorChanged
signal to the function that sets the value of the zoom selector:
connect(ui->pdfView, &QPdfView::zoomFactorChanged, m_zoomSelector, &ZoomSelector::setZoomFactor); }