aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml/chapter2-methods
diff options
context:
space:
mode:
Diffstat (limited to 'examples/qml/tutorials/extending-qml/chapter2-methods')
-rw-r--r--examples/qml/tutorials/extending-qml/chapter2-methods/app.qml3
-rw-r--r--examples/qml/tutorials/extending-qml/chapter2-methods/doc/chapter2-methods.rst36
2 files changed, 37 insertions, 2 deletions
diff --git a/examples/qml/tutorials/extending-qml/chapter2-methods/app.qml b/examples/qml/tutorials/extending-qml/chapter2-methods/app.qml
index d330f3b64..d9477e253 100644
--- a/examples/qml/tutorials/extending-qml/chapter2-methods/app.qml
+++ b/examples/qml/tutorials/extending-qml/chapter2-methods/app.qml
@@ -1,6 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-//![0]
+
import Charts
import QtQuick
@@ -30,4 +30,3 @@ Item {
text: "Click anywhere to clear the chart"
}
}
-//![0]
diff --git a/examples/qml/tutorials/extending-qml/chapter2-methods/doc/chapter2-methods.rst b/examples/qml/tutorials/extending-qml/chapter2-methods/doc/chapter2-methods.rst
new file mode 100644
index 000000000..245d0ddb2
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml/chapter2-methods/doc/chapter2-methods.rst
@@ -0,0 +1,36 @@
+.. _qml-chapter2-methods:
+
+Extending QML - Connecting to C++ Methods and Signals
+=====================================================
+
+This is the second of a series of 6 examples forming a tutorial about extending
+QML with Python.
+
+Suppose we want ``PieChart`` to have a ``clearChart()`` method that erases the
+chart and then emits a ``chartCleared`` signal. Our ``app.qml`` would be able
+to call ``clearChart()`` and receive ``chartCleared()`` signals like this:
+
+.. literalinclude:: app.qml
+ :lineno-start: 4
+ :lines: 4-32
+
+To do this, we add a ``clearChart()`` method and a ``chartCleared()`` signal
+to our C++ class:
+
+.. literalinclude:: methods.py
+ :lineno-start: 54
+ :lines: 54-58
+
+The use of the ``Slot`` decorator makes the ``clearChart()`` method available
+to the Qt Meta-Object system, and in turn, to QML. The method simply changes
+the color to ``Qt::transparent``, repaints the chart, then emits the
+``chartCleared()`` signal:
+
+.. literalinclude:: methods.py
+ :lineno-start: 21
+ :lines: 21-24
+
+Now when we run the application and click the window, the pie chart disappears,
+and the application outputs::
+
+ qml: The chart has been cleared