diff options
author | Friedemann Kleint <[email protected]> | 2021-02-10 08:38:34 +0100 |
---|---|---|
committer | Friedemann Kleint <[email protected]> | 2021-02-10 11:18:21 +0100 |
commit | a3a3cc0165ef700328a5078e01e503f5eb9fd2fa (patch) | |
tree | d8ebd789ee6fbdb8b5588f184a68b69d44a65540 /examples/declarative | |
parent | ae8f5327e21786690e021344cf2c122a02c89440 (diff) |
Port QML examples to new property decorators
Task-number: PYSIDE-1019
Change-Id: I322c1d4d0f785b889d0676f7b9f292becd25e82f
Reviewed-by: Christian Tismer <[email protected]>
Diffstat (limited to 'examples/declarative')
5 files changed, 70 insertions, 55 deletions
diff --git a/examples/declarative/extending/chapter1-basics/basics.py b/examples/declarative/extending/chapter1-basics/basics.py index c14669908..5e2614fc7 100644 --- a/examples/declarative/extending/chapter1-basics/basics.py +++ b/examples/declarative/extending/chapter1-basics/basics.py @@ -44,15 +44,19 @@ import os import sys -from PySide6.QtCore import Property, Signal, QUrl +from PySide6.QtCore import Property, Signal, Qt, QUrl from PySide6.QtGui import QGuiApplication, QPen, QPainter, QColor from PySide6.QtQml import qmlRegisterType from PySide6.QtQuick import QQuickPaintedItem, QQuickView class PieChart (QQuickPaintedItem): + + nameChanged = Signal() + def __init__(self, parent = None): QQuickPaintedItem.__init__(self, parent) self._name = u'' + self._color = QColor() def paint(self, painter): pen = QPen(self.color, 2) @@ -60,22 +64,22 @@ class PieChart (QQuickPaintedItem): painter.setRenderHints(QPainter.Antialiasing, True) painter.drawPie(self.boundingRect().adjusted(1,1,-1,-1), 90 * 16, 290 * 16) - def getColor(self): + @Property(QColor) + def color(self): return self._color - def setColor(self, value): + @color.setter + def color(self, value): self._color = value - def getName(self): + @Property(str, notify=nameChanged) + def name(self): return self._name - def setName(self, value): + @name.setter + def name(self, value): self._name = value - nameChanged = Signal() - - color = Property(QColor, getColor, setColor) - name = Property(str, getName, setName, notify=nameChanged) if __name__ == '__main__': app = QGuiApplication(sys.argv) diff --git a/examples/declarative/extending/chapter2-methods/methods.py b/examples/declarative/extending/chapter2-methods/methods.py index b10cab324..8b9c94c62 100644 --- a/examples/declarative/extending/chapter2-methods/methods.py +++ b/examples/declarative/extending/chapter2-methods/methods.py @@ -44,15 +44,20 @@ import os import sys -from PySide6.QtCore import Property, Signal, Slot, QUrl, Qt +from PySide6.QtCore import Property, Signal, Slot, Qt, QUrl from PySide6.QtGui import QGuiApplication, QPen, QPainter, QColor from PySide6.QtQml import qmlRegisterType from PySide6.QtQuick import QQuickPaintedItem, QQuickView class PieChart (QQuickPaintedItem): + + chartCleared = Signal() + nameChanged = Signal() + def __init__(self, parent = None): QQuickPaintedItem.__init__(self, parent) self._name = u'' + self._color = QColor() def paint(self, painter): pen = QPen(self.color, 2) @@ -60,25 +65,25 @@ class PieChart (QQuickPaintedItem): painter.setRenderHints(QPainter.Antialiasing, True) painter.drawPie(self.boundingRect().adjusted(1,1,-1,-1), 90 * 16, 290 * 16) - def getColor(self): + @Property(QColor) + def color(self): return self._color - def setColor(self, value): + @color.setter + def color(self, value): self._color = value - def getName(self): + @Property(str, notify=nameChanged) + def name(self): return self._name - def setName(self, value): + @name.setter + def name(self, value): self._name = value - color = Property(QColor, getColor, setColor) - name = Property(str, getName, setName) - chartCleared = Signal() - @Slot() # This should be something like @Invokable def clearChart(self): - self.setColor(Qt.transparent) + self.color = Qt.transparent self.update() self.chartCleared.emit() diff --git a/examples/declarative/extending/chapter3-bindings/bindings.py b/examples/declarative/extending/chapter3-bindings/bindings.py index 2c1546927..474745652 100644 --- a/examples/declarative/extending/chapter3-bindings/bindings.py +++ b/examples/declarative/extending/chapter3-bindings/bindings.py @@ -50,6 +50,11 @@ from PySide6.QtQml import qmlRegisterType from PySide6.QtQuick import QQuickPaintedItem, QQuickView class PieChart (QQuickPaintedItem): + + chartCleared = Signal() + nameChanged = Signal() + colorChanged = Signal() + def __init__(self, parent = None): QQuickPaintedItem.__init__(self, parent) self._name = u'' @@ -61,29 +66,28 @@ class PieChart (QQuickPaintedItem): painter.setRenderHints(QPainter.Antialiasing, True) painter.drawPie(self.boundingRect().adjusted(1,1,-1,-1), 90 * 16, 290 * 16) - def getColor(self): + @Property(QColor, notify=colorChanged) + def color(self): return self._color - def setColor(self, value): + @color.setter + def color(self, value): if value != self._color: self._color = value self.update() self.colorChanged.emit() - def getName(self): + @Property(str, notify=nameChanged) + def name(self): return self._name - def setName(self, value): + @name.setter + def name(self, value): self._name = value - colorChanged = Signal() - color = Property(QColor, getColor, setColor, notify=colorChanged) - name = Property(str, getName, setName) - chartCleared = Signal() - @Slot() # This should be something like @Invokable def clearChart(self): - self.setColor(Qt.transparent) + self.color = Qt.transparent self.update() self.chartCleared.emit() diff --git a/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py b/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py index 63c4e4959..6a4be313e 100644 --- a/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py +++ b/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py @@ -55,14 +55,14 @@ class PieSlice (QQuickPaintedItem): QQuickPaintedItem.__init__(self, parent) self._color = QColor() - def getColor(self): + @Property(QColor) + def color(self): return self._color - def setColor(self, value): + @color.setter + def color(self, value): self._color = value - color = Property(QColor, getColor, setColor) - def paint(self, painter): pen = QPen(self._color, 2) painter.setPen(pen) @@ -75,23 +75,23 @@ class PieChart (QQuickItem): self._name = None self._pieSlice = None - def getName(self): + @Property(str) + def name(self): return self._name - def setName(self, value): + @name.setter + def name(self, value): self._name = value - name = Property(str, getName, setName) - - def getPieSlice(self): + @Property(PieSlice) + def pieSlice(self): return self._pieSlice - def setPieSlice(self, value): + @pieSlice.setter + def pieSlice(self, value): self._pieSlice = value self._pieSlice.setParentItem(self) - pieSlice = Property(PieSlice, getPieSlice, setPieSlice) - if __name__ == '__main__': app = QGuiApplication(sys.argv) diff --git a/examples/declarative/extending/chapter5-listproperties/listproperties.py b/examples/declarative/extending/chapter5-listproperties/listproperties.py index 466b3aca1..37603ea12 100644 --- a/examples/declarative/extending/chapter5-listproperties/listproperties.py +++ b/examples/declarative/extending/chapter5-listproperties/listproperties.py @@ -56,28 +56,30 @@ class PieSlice (QQuickPaintedItem): self._fromAngle = 0 self._angleSpan = 0 - def getColor(self): + @Property(QColor) + def color(self): return self._color - def setColor(self, value): + @color.setter + def color(self, value): self._color = value - def getFromAngle(self): + @Property(int) + def fromAngle(self): return self._angle - def setFromAngle(self, value): + @fromAngle.setter + def fromAngle(self, value): self._fromAngle = value - def getAngleSpan(self): + @Property(int) + def angleSpan(self): return self._angleSpan - def setAngleSpan(self, value): + @angleSpan.setter + def angleSpan(self, value): self._angleSpan = value - color = Property(QColor, getColor, setColor) - fromAngle = Property(int, getFromAngle, setFromAngle) - angleSpan = Property(int, getAngleSpan, setAngleSpan) - def paint(self, painter): pen = QPen(self._color, 2) painter.setPen(pen) @@ -90,14 +92,14 @@ class PieChart (QQuickItem): self._name = u'' self._slices = [] - def getName(self): + @Property(str) + def name(self): return self._name - def setName(self, value): + @name.setter + def name(self, value): self._name = value - name = Property(str, getName, setName) - def appendSlice(self, _slice): _slice.setParentItem(self) self._slices.append(_slice) |