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
51
52
53
54
55
56
57
58
59
60
|
# Copyright (C) 2024 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import sys
from pathlib import Path
from PySide6.QtCore import QCoreApplication, QUrl, QRect, QPoint
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtWebView import QtWebView
import argparse
import rc_qml # noqa: F401
class Utils:
@staticmethod
def fromUserInput(userInput):
if not userInput:
return QUrl.fromUserInput("about:blank")
result = QUrl.fromUserInput(userInput)
return result if result.isValid() else QUrl.fromUserInput("about:blank")
if __name__ == "__main__":
QtWebView.initialize()
app = QGuiApplication(sys.argv)
QGuiApplication.setApplicationDisplayName(QCoreApplication
.translate("main", "QtWebView Example"))
parser = argparse.ArgumentParser(description=QGuiApplication.applicationDisplayName())
parser.add_argument("--url", nargs="?",
default="https://www.qt.io",
help="The initial URL to open.")
args = parser.parse_args()
initialUrl = args.url
engine = QQmlApplicationEngine()
context = engine.rootContext()
context.setContextProperty("utils", Utils())
context.setContextProperty("initialUrl", Utils.fromUserInput(initialUrl))
geometry = QGuiApplication.primaryScreen().availableGeometry()
if not QGuiApplication.styleHints().showIsFullScreen():
size = geometry.size() * 4 / 5
offset = (geometry.size() - size) / 2
pos = geometry.topLeft() + QPoint(offset.width(), offset.height())
geometry = QRect(pos, size)
engine.setInitialProperties({"x": geometry.x(), "y": geometry.y(),
"width": geometry.width(), "height": geometry.height()})
qml_file = Path(__file__).parent / "main.qml"
engine.load(QUrl.fromLocalFile(qml_file))
if not engine.rootObjects():
sys.exit(-1)
exit_code = app.exec()
del engine
sys.exit(exit_code)
|