diff options
author | Hugo Parente Lima <hugo.pl@gmail.com> | 2010-12-03 17:51:46 -0200 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2010-12-03 17:51:46 -0200 |
commit | 5b0ec63be2167e27461c878969a39e9caff3968e (patch) | |
tree | da71c875065cd7148fd70dbbb03efd6f9cad9288 /examples | |
parent | 745f31ab32639c9b26fae15e07f065b5fe455043 (diff) |
QML registerType examples.
Diffstat (limited to 'examples')
8 files changed, 471 insertions, 0 deletions
diff --git a/examples/declarative/extending/chapter1-basics/app.qml b/examples/declarative/extending/chapter1-basics/app.qml new file mode 100644 index 0000000..4d67fc6 --- /dev/null +++ b/examples/declarative/extending/chapter1-basics/app.qml @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +****************************************************************************/ +//![0] +import Charts 1.0 +import Qt 4.7 + +Item { + width: 300; height: 200 + + PieChart { + id: aPieChart + anchors.centerIn: parent + width: 100; height: 100 + name: "A simple pie chart" + color: "red" + } + + Text { + anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 } + text: aPieChart.name + } +} +//![0] diff --git a/examples/declarative/extending/chapter1-basics/basics.py b/examples/declarative/extending/chapter1-basics/basics.py new file mode 100644 index 0000000..e7957e1 --- /dev/null +++ b/examples/declarative/extending/chapter1-basics/basics.py @@ -0,0 +1,44 @@ + +import sys +from PySide.QtCore import * +from PySide.QtGui import * +from PySide.QtDeclarative import * + +class PieChart (QDeclarativeItem): + + def __init__(self, parent = None): + QDeclarativeItem.__init__(self, parent) + # need to disable this flag to draw inside a QDeclarativeItem + self.setFlag(QGraphicsItem.ItemHasNoContents, False) + self._name = u'' + + def paint(self, painter, options, widget): + pen = QPen(self.color, 2) + painter.setPen(pen); + painter.setRenderHints(QPainter.Antialiasing, True); + painter.drawPie(self.boundingRect(), 90 * 16, 290 * 16); + + def getColor(self): + return self._color + + def setColor(self, value): + self._color = value + + def getName(self): + return self._name + + def setName(self, value): + self._name = value + + color = Property(QColor, getColor, setColor) + name = Property(unicode, getName, setName) + +if __name__ == '__main__': + app = QApplication(sys.argv) + + qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); + + view = QDeclarativeView() + view.setSource(QUrl.fromLocalFile('app.qml')) + view.show() + sys.exit(app.exec_()) diff --git a/examples/declarative/extending/chapter2-methods/app.qml b/examples/declarative/extending/chapter2-methods/app.qml new file mode 100644 index 0000000..7f21320 --- /dev/null +++ b/examples/declarative/extending/chapter2-methods/app.qml @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +****************************************************************************/ +//![0] +import Charts 1.0 +import Qt 4.7 + +Item { + width: 300; height: 200 + + PieChart { + id: aPieChart + anchors.centerIn: parent + width: 100; height: 100 + color: "red" + + onChartCleared: console.log("The chart has been cleared") + } + + MouseArea { + anchors.fill: parent + onClicked: aPieChart.clearChart() + } + + Text { + anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 } + text: "Click anywhere to clear the chart" + } +} +//![0] diff --git a/examples/declarative/extending/chapter2-methods/methods.py b/examples/declarative/extending/chapter2-methods/methods.py new file mode 100644 index 0000000..d54b55a --- /dev/null +++ b/examples/declarative/extending/chapter2-methods/methods.py @@ -0,0 +1,52 @@ + +import sys +from PySide.QtCore import * +from PySide.QtGui import * +from PySide.QtDeclarative import * + +class PieChart (QDeclarativeItem): + + def __init__(self, parent = None): + QDeclarativeItem.__init__(self, parent) + # need to disable this flag to draw inside a QDeclarativeItem + self.setFlag(QGraphicsItem.ItemHasNoContents, False) + self._name = u'' + + def paint(self, painter, options, widget): + pen = QPen(self.color, 2) + painter.setPen(pen); + painter.setRenderHints(QPainter.Antialiasing, True); + painter.drawPie(self.boundingRect(), 90 * 16, 290 * 16); + + def getColor(self): + return self._color + + def setColor(self, value): + self._color = value + + def getName(self): + return self._name + + def setName(self, value): + self._name = value + + color = Property(QColor, getColor, setColor) + name = Property(unicode, getName, setName) + + chartCleared = Signal() + + @Slot() # This should be something like @Invokable + def clearChart(self): + self.setColor(Qt.transparent) + self.update() + self.chartCleared.emit() + +if __name__ == '__main__': + app = QApplication(sys.argv) + + qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); + + view = QDeclarativeView() + view.setSource(QUrl.fromLocalFile('app.qml')) + view.show() + sys.exit(app.exec_()) diff --git a/examples/declarative/extending/chapter3-bindings/app.qml b/examples/declarative/extending/chapter3-bindings/app.qml new file mode 100644 index 0000000..2ff6ae1 --- /dev/null +++ b/examples/declarative/extending/chapter3-bindings/app.qml @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import Charts 1.0 +import Qt 4.7 + +Item { + width: 300; height: 200 + + Row { + anchors.centerIn: parent + spacing: 20 + + PieChart { + id: chartA + width: 100; height: 100 + color: "red" + } + + PieChart { + id: chartB + width: 100; height: 100 + color: chartA.color + } + } + + MouseArea { + anchors.fill: parent + onClicked: { chartA.color = "blue" } + } + + Text { + anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 } + text: "Click anywhere to change the chart color" + } +} +//![0] diff --git a/examples/declarative/extending/chapter3-bindings/bindings.py b/examples/declarative/extending/chapter3-bindings/bindings.py new file mode 100644 index 0000000..d0fe24d --- /dev/null +++ b/examples/declarative/extending/chapter3-bindings/bindings.py @@ -0,0 +1,57 @@ + +import sys +from PySide.QtCore import * +from PySide.QtGui import * +from PySide.QtDeclarative import * + +class PieChart (QDeclarativeItem): + + def __init__(self, parent = None): + QDeclarativeItem.__init__(self, parent) + # need to disable this flag to draw inside a QDeclarativeItem + self.setFlag(QGraphicsItem.ItemHasNoContents, False) + self._name = u'' + self._color = QColor() + + def paint(self, painter, options, widget): + pen = QPen(self._color, 2) + painter.setPen(pen); + painter.setRenderHints(QPainter.Antialiasing, True); + painter.drawPie(self.boundingRect(), 90 * 16, 290 * 16); + + def getColor(self): + return self._color + + def setColor(self, value): + if value != self._color: + self._color = value + self.update() + self.colorChanged.emit() + + def getName(self): + return self._name + + def setName(self, value): + self._name = value + + colorChanged = Signal() + color = Property(QColor, getColor, setColor, notify=colorChanged) + name = Property(unicode, getName, setName) + + chartCleared = Signal() + + @Slot() # This should be something like @Invokable + def clearChart(self): + self.setColor(Qt.transparent) + self.update() + self.chartCleared.emit() + +if __name__ == '__main__': + app = QApplication(sys.argv) + + qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); + + view = QDeclarativeView() + view.setSource(QUrl.fromLocalFile('app.qml')) + view.show() + sys.exit(app.exec_()) diff --git a/examples/declarative/extending/chapter4-customPropertyTypes/app.qml b/examples/declarative/extending/chapter4-customPropertyTypes/app.qml new file mode 100644 index 0000000..fcd3806 --- /dev/null +++ b/examples/declarative/extending/chapter4-customPropertyTypes/app.qml @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import Charts 1.0 +import Qt 4.7 + +Item { + width: 300; height: 200 + + PieChart { + id: chart + anchors.centerIn: parent + width: 100; height: 100 + + pieSlice: PieSlice { + anchors.fill: parent + color: "red" + } + } + + Component.onCompleted: console.log("The pie is colored " + chart.pieSlice.color) +} +//![0] diff --git a/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py b/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py new file mode 100644 index 0000000..0aff433 --- /dev/null +++ b/examples/declarative/extending/chapter4-customPropertyTypes/customPropertyTypes.py @@ -0,0 +1,62 @@ + +import sys +from PySide.QtCore import * +from PySide.QtGui import * +from PySide.QtDeclarative import * + +class PieSlice (QDeclarativeItem): + + def __init__(self, parent = None): + QDeclarativeItem.__init__(self, parent) + # need to disable this flag to draw inside a QDeclarativeItem + self.setFlag(QGraphicsItem.ItemHasNoContents, False) + self._color = QColor() + + def getColor(self): + return self._color + + def setColor(self, value): + self._color = value + + color = Property(QColor, getColor, setColor) + + def paint(self, painter, options, widget): + pen = QPen(self._color, 2) + painter.setPen(pen); + painter.setRenderHints(QPainter.Antialiasing, True); + painter.drawPie(self.boundingRect(), 90 * 16, 290 * 16); + +class PieChart (QDeclarativeItem): + + def __init__(self, parent = None): + QDeclarativeItem.__init__(self, parent) + self._name = u'' + self._pieSlice = None + + def getName(self): + return self._name + + def setName(self, value): + self._name = value + + name = Property(unicode, getName, setName) + + def getPieSlice(self): + return self._pieSlice + + def setPieSlice(self, value): + self._pieSlice = value + self._pieSlice.setParentItem(self) + + pieSlice = Property(PieSlice, getPieSlice, setPieSlice) + +if __name__ == '__main__': + app = QApplication(sys.argv) + + qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); + qmlRegisterType(PieSlice, "Charts", 1, 0, "PieSlice"); + + view = QDeclarativeView() + view.setSource(QUrl.fromLocalFile('app.qml')) + view.show() + sys.exit(app.exec_()) |