Qt Pdf Viewer Library Installation Instructions

Usage

To use the qt-pdf-viewer-library in your app, follow these steps:

  • include the library in your .pro:
QML_IMPORT_PATH += $$PWD/libs/qt-pdf-viewer-library/
QML_DESIGNER_IMPORT_PATH += $$PWD/libs/qt-pdf-viewer-library/
include($$PWD/libs/qt-pdf-viewer-library/qtpdfviewer.pri)
  • initialize the library in your main.cpp just before QGuiApplication instantiation, and connect the aboutToQuit signal, emitted on app close, with the QtPdfViewerInitializer singleton deleteInstance method to allow the correct deletion of the initializer instance:
int main(int argc, char *argv[])
{
  ...

  // Initialize QtPdfViewer library
  // To make the pdf module to function correctly across all platforms,
  // it is necessary to call QtPdfViewerInitializer::initialize() before creating
  // the QGuiApplication instance.
  LTDev::QtPdfViewerInitializer::initialize();

  QGuiApplication app(argc, argv);

  // Delete QtPdfViewer instance on app close
  QObject::connect(&app, &QGuiApplication::aboutToQuit, LTDev::QtPdfViewerInitializer::getInstance(), LTDev::QtPdfViewerInitializer::deleteInstance);

  ...
}
  • then, in qml import the library import it.ltdev.qt.qml.components 1.0, and use the provided PdfView class.
import it.ltdev.qt.qml.components 1.0 as LTDev

ApplicationWindow {
      ...

      LTDev.PdfView {
          id: pdfView

          anchors.fill: parent

          onViewerLoaded: {
              // Load pdf only when viewer is ready
              pdfView.load("path/to/my/document.pdf")
          }
      }
}
  • (Android only) since Android 9 or higher, in order to view remote document from an HTTP url, is necessary to add android:usesCleartextTraffic="true" in the application tag of the manifest.xml. Since that version cleartext support is disabled by default.
<application android:usesCleartextTraffic="true" ...>
  • (Android only) on Android permission android.permission.READ_EXTERNAL_STORAGE must be added in the manifest.xml, and since Android 9, it must be also asked at runtime (see sample app).
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

 



Back to Qt Marketplace