aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pdf/quickpdfviewer/main.py
blob: 388c590c31fc7a2ac3c4e132dc5022f362f5e841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations

import os
import sys
from argparse import ArgumentParser, RawTextHelpFormatter
from pathlib import Path

from PySide6.QtQml import QQmlApplicationEngine

from PySide6.QtGui import QGuiApplication
from PySide6.QtCore import QCoreApplication, QUrl

import rc_viewer  # noqa: F401

"""PySide6 port of the pdf/pdfviewer example from Qt v6.x"""


if __name__ == "__main__":
    name = "Qt Quick PDF Viewer Example"
    QCoreApplication.setApplicationName(name)
    QCoreApplication.setOrganizationName("QtProject")

    app = QGuiApplication(sys.argv)

    dir = Path(__file__).resolve().parent

    argument_parser = ArgumentParser(description=name,
                                     formatter_class=RawTextHelpFormatter)
    argument_parser.add_argument("file", help="The file to open",
                                 nargs='?', type=str)
    options = argument_parser.parse_args()

    url = None
    if options.file:
        url = QUrl.fromLocalFile(options.file)
    else:
        url = QUrl.fromLocalFile(os.fspath(dir / "resources" / "test.pdf"))

    engine = QQmlApplicationEngine()
    engine.setInitialProperties({"source": url})

    engine.load(QUrl.fromLocalFile(os.fspath(dir / "viewer.qml")))
    if not engine.rootObjects():
        sys.exit(-1)

    exit_code = QCoreApplication.exec()
    del engine
    sys.exit(exit_code)