PDF Viewer Example - QT PDF 5.15.5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

7/9/2021 PDF Viewer Example | Qt PDF 5.15.

We bake cookies in your browser for a better experience. Using this site means that you consent.
Read
more
Continue »
Contents

 Running the Example


Creating the Main Window
Blog
Contact Us

Wiki Files and Attributions



Documentation
Forum
Bug Reports
Code Review
Resource Center
Qt Extensions

Qt Documentation

Search Documentation Previous


Qt PDF Examples
Qt 5.15 ›
Qt PDF ›
Qt PDF Examples ›
PDF Viewer Example

Reference
All Qt C++ Classes
All QML Types
All Qt Modules
Qt Creator Manual
All Qt Reference Documentation

Getting Started
Getting Started with Qt
What's New in Qt 5
Examples and Tutorials
Supported Platforms
Qt Licensing

Overviews
Development Tools
User Interfaces
Core Internals
Data Storage

Multimedia
Networking and Connectivity
Graphics
Mobile APIs
QML Applications
All Qt Overviews

https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 1/6
7/9/2021 PDF Viewer Example | Qt PDF 5.15.5

PDF Viewer Example


Renders PDF documents.

[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: Creating a Qt Widget Based Application.

Running the Example


To run the example from Qt Creator, open the Welcome mode and select the example from
Examples. For more information, visit Building and Running an Example.

Creating the Main Window

The MainWindow class inherits the QMainWindow class:

class MainWindow : public QMainWindow

Q_OBJECT

public:

explicit MainWindow(QWidget *parent = nullptr);

~MainWindow();

The class declares public and private slots that match the actions of the selectors:

public slots:

void open(const QUrl &docLocation);

i l
https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 2/6
7/9/2021 PDF Viewer Example | Qt PDF 5.15.5
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;

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.

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

The constructor first calls setupUi() to construct the zoom and page selectors according to the
UI file. We set the maximum width of the selectors.

ui->setupUi(this);

https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 3/6
7/9/2021 PDF Viewer Example | Qt PDF 5.15.5

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

We connect the 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::


connect(m_zoomSelector, &ZoomSelector::zoomFactorChanged, ui->pdfView, &QPdfView
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);

Files and Attributions

Files:

pdfviewer/main.cpp
pdfviewer/mainwindow.cpp
pdfviewer/mainwindow.h
pdfviewer/mainwindow.ui
pdfviewer/pageselector.cpp
pdfviewer/pageselector h
https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 4/6
7/9/2021 PDF Viewer Example | Qt PDF 5.15.5
pdfviewer/pageselector.h
pdfviewer/pdfviewer.pro
pdfviewer/resources.qrc
pdfviewer/zoomselector.cpp
pdfviewer/zoomselector.h

Images:

pdfviewer/images/busy.png
pdfviewer/images/fileopen.png
pdfviewer/images/go-next-24.png
pdfviewer/images/go-previous-24.png
pdfviewer/images/zoom-in-24.png
pdfviewer/images/zoom-in-32.png
pdfviewer/images/zoom-out-24.png
pdfviewer/images/zoom-out-32.png

© 2021 The Qt Company Ltd.


Documentation contributions included herein are the copyrights of
their respective owners. The
documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by
the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries
worldwide. All other trademarks are property
of their respective owners.

Download Product Services

Start for Free Qt in Use Technology Evaluation


Qt for Application Development Qt for Application Development Proof of Concept
Qt for Device Creation Qt for Device Creation Design & Implementation
Qt Open Source Commercial Features Productization
Terms & Conditions Qt Creator IDE Qt Training
Licensing FAQ Qt Quick Partner Network

Developers About us

Qt Extensions Training & Events


Examples & Tutorials Resource Center
Development Tools News
Wiki Careers
Forums Locations
Contribute to Qt Contact Us




https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 5/6
7/9/2021 PDF Viewer Example | Qt PDF 5.15.5

Sign In Feedback © 2020 The Qt Company

https://doc.qt.io/qt-5/qtpdf-pdfviewer-example.html 6/6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy