aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml-advanced/extended/main.py
blob: 6ee386401a2f54f2fddb928aec65d44ca8b46d35 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

"""PySide6 port of the qml/examples/qml/referenceexamples/extended example from Qt v6.x"""

from pathlib import Path
import sys

from PySide6.QtCore import QObject, QUrl, Property
from PySide6.QtWidgets import QApplication, QLineEdit
from PySide6.QtQml import (QQmlComponent, QQmlEngine, QmlForeign, QmlExtended,
                           QmlNamedElement)


# To be used on the @QmlElement decorator
# (QML_IMPORT_MINOR_VERSION is optional)
QML_IMPORT_NAME = "examples.extend"
QML_IMPORT_MAJOR_VERSION = 1


class LineEditExtension(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)
        self._line_edit = parent

    @Property(int)
    def left_margin(self):
        return self._line_edit.textMargins().left()

    @left_margin.setter
    def left_margin(self, m):
        margins = self._line_edit.textMargins()
        margins.setLeft(m)
        self._line_edit.setTextMargins(margins)

    @Property(int)
    def right_margin(self):
        return self._line_edit.textMargins().right()

    @right_margin.setter
    def right_margin(self, m):
        margins = self._line_edit.textMargins()
        margins.setRight(m)
        self._line_edit.setTextMargins(margins)

    @Property(int)
    def top_margin(self):
        return self._line_edit.textMargins().top()

    @top_margin.setter
    def top_margin(self, m):
        margins = self._line_edit.textMargins()
        margins.setTop(m)
        self._line_edit.setTextMargins(margins)

    @Property(int)
    def bottom_margin(self):
        return self._line_edit.textMargins().bottom()

    @bottom_margin.setter
    def bottom_margin(self, m):
        margins = self._line_edit.textMargins()
        margins.setBottom(m)
        self._line_edit.setTextMargins(margins)


@QmlNamedElement("QLineEdit")
@QmlExtended(LineEditExtension)
@QmlForeign(QLineEdit)
class LineEditForeign(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    qml_file = Path(__file__).parent / "example.qml"
    url = QUrl.fromLocalFile(qml_file)
    engine = QQmlEngine()
    component = QQmlComponent(engine, url)
    widget = component.create()
    if not widget:
        print(component.errors())
        del engine
        sys.exit(-1)

    widget.show()
    r = app.exec()
    # Deleting the engine before it goes out of scope is required to make sure
    # all child QML instances are destroyed in the correct order.
    del engine
    sys.exit(r)