PDF Viewer Example▲
PDF Viewer demonstrates how to use the QPdfView class to render PDF documents and the QPdfPageNavigator 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 &
amp;docLocation);
private
slots:
void
bookmarkSelected(const
QModelIndex &
amp;index);
void
pageSelected(int
page);
// 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();
void
on_actionBack_triggered();
void
on_actionForward_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;
QSpinBox *
m_pageSelector;
QFileDialog *
m_fileDialog =
nullptr
;
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
QSpinBox(this
))
, m_document(new
QPdfDocument(this
))
{
The constructor first calls setupUi() to construct most of the main window according to the UI file. The zoom and page selectors need to be added to the toolbar via QToolBar::insertWidget(), because that cannot be done in a UI file:
ui-&
gt;setupUi(this
);
m_zoomSelector-&
gt;setMaximumWidth(150
);
ui-&
gt;mainToolBar-&
gt;insertWidget(ui-&
gt;actionZoom_In, m_zoomSelector);
ui-&
gt;mainToolBar-&
gt;insertWidget(ui-&
gt;actionForward, m_pageSelector);
We connect relevant signals to the page selector spinbox and the browser-style back and forward buttons:
connect(m_pageSelector, &
amp;QSpinBox::
valueChanged, this
, &
amp;MainWindow::
pageSelected);
auto
nav =
ui-&
gt;pdfView-&
gt;pageNavigator();
connect(nav, &
amp;QPdfPageNavigator::
currentPageChanged, m_pageSelector, &
amp;QSpinBox::
setValue);
connect(nav, &
amp;QPdfPageNavigator::
backAvailableChanged, ui-&
gt;actionBack, &
amp;QAction::
setEnabled);
connect(nav, &
amp;QPdfPageNavigator::
forwardAvailableChanged, ui-&
gt;actionForward, &
amp;QAction::
setEnabled);
We connect the zoomModeChanged and zoomFactor changed signals of the PDF view to the functions that reset the zoom selector:
connect(m_zoomSelector, &
amp;ZoomSelector::
zoomModeChanged, ui-&
gt;pdfView, &
amp;QPdfView::
setZoomMode);
connect(m_zoomSelector, &
amp;ZoomSelector::
zoomFactorChanged, ui-&
gt;pdfView, &
amp;QPdfView::
setZoomFactor);
m_zoomSelector-&
gt;reset();
We then load the PDF document to the viewer:
...
ui-&
gt;pdfView-&
gt;setDocument(m_document);
Finally, we connect the zoomFactorChanged signal to the function that sets the value of the zoom selector:
connect(ui-&
gt;pdfView, &
amp;QPdfView::
zoomFactorChanged,
m_zoomSelector, &
amp;ZoomSelector::
setZoomFactor);
}