diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2019-07-09 09:12:01 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2019-07-10 09:24:27 +0000 |
commit | c27554068efe63d2093d6a28cc83cb95190003a2 (patch) | |
tree | 0dbe40277a7c5d3c988630d15ef40c5d2dd96da2 /src | |
parent | 1c84e6debfc05e60f1f331ba5f1f265ec6590a8b (diff) |
Fix docs to explain the new type syntax for QML methods
[ChangeLog][QtQml] It is now possible to specify types for method
parameters and their return value in QML (basic and object types), using
TypeScript-like syntax with a colon separator. The syntax for QML
declared signals supports the same style.
This change also adapts the remaining snippets and docs to the "fresher"
qml signal parameter syntax.
Change-Id: I601781f01f696276951b04785584adab39fedfd9
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src')
7 files changed, 31 insertions, 28 deletions
diff --git a/src/qml/doc/snippets/qml/events.qml b/src/qml/doc/snippets/qml/events.qml index 90bf5d7b3d..f437e32890 100644 --- a/src/qml/doc/snippets/qml/events.qml +++ b/src/qml/doc/snippets/qml/events.qml @@ -59,8 +59,8 @@ Rectangle { //! [signal declaration] signal trigger - signal send (string notice) - signal perform (string task, variant object) + signal send(notice: string) + signal perform(task: string, object: variant) //! [signal declaration] //! [signal handler declaration] @@ -88,7 +88,7 @@ Rectangle { Rectangle { id: messenger - signal send( string person, string notice) + signal send(person: string, notice: string) onSend: { console.log("For " + person + ", the notice is: " + notice) @@ -102,7 +102,7 @@ Rectangle { Rectangle { id: relay - signal send( string person, string notice) + signal send(person: string, notice: string) onSend: console.log("Send signal to: " + person + ", " + notice) Component.onCompleted: { diff --git a/src/qml/doc/snippets/qml/qtbinding/functions-qml/MyItem.qml b/src/qml/doc/snippets/qml/qtbinding/functions-qml/MyItem.qml index 073a5dc361..59907c38e7 100644 --- a/src/qml/doc/snippets/qml/qtbinding/functions-qml/MyItem.qml +++ b/src/qml/doc/snippets/qml/qtbinding/functions-qml/MyItem.qml @@ -52,7 +52,7 @@ import QtQuick 2.0 Item { - function myQmlFunction(msg) { + function myQmlFunction(msg: string) : string { console.log("Got message:", msg) return "some return value" } diff --git a/src/qml/doc/snippets/qml/qtbinding/functions-qml/main.cpp b/src/qml/doc/snippets/qml/qtbinding/functions-qml/main.cpp index c82f71f749..a562eae2b4 100644 --- a/src/qml/doc/snippets/qml/qtbinding/functions-qml/main.cpp +++ b/src/qml/doc/snippets/qml/qtbinding/functions-qml/main.cpp @@ -60,13 +60,13 @@ QQmlEngine engine; QQmlComponent component(&engine, "MyItem.qml"); QObject *object = component.create(); -QVariant returnedValue; -QVariant msg = "Hello from C++"; +QString returnedValue; +QString msg = "Hello from C++"; QMetaObject::invokeMethod(object, "myQmlFunction", - Q_RETURN_ARG(QVariant, returnedValue), - Q_ARG(QVariant, msg)); + Q_RETURN_ARG(QString, returnedValue), + Q_ARG(QString, msg)); -qDebug() << "QML function returned:" << returnedValue.toString(); +qDebug() << "QML function returned:" << returnedValue; delete object; //![0] } diff --git a/src/qml/doc/snippets/qml/qtbinding/signals-qml/MyItem.qml b/src/qml/doc/snippets/qml/qtbinding/signals-qml/MyItem.qml index eebf2db832..aadc89b72c 100644 --- a/src/qml/doc/snippets/qml/qtbinding/signals-qml/MyItem.qml +++ b/src/qml/doc/snippets/qml/qtbinding/signals-qml/MyItem.qml @@ -55,7 +55,7 @@ Item { id: item width: 100; height: 100 - signal qmlSignal(string msg) + signal qmlSignal(msg: string) MouseArea { anchors.fill: parent diff --git a/src/qml/doc/snippets/qml/statemachine/guardcondition.qml b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml index 8388b96c21..f1ec89b6ba 100644 --- a/src/qml/doc/snippets/qml/statemachine/guardcondition.qml +++ b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml @@ -70,7 +70,7 @@ Rectangle { } } // define the signal the SignalTransition is connected with - signal mysignal(string mystr) + signal mysignal(mystr: string) // on clicking the button emit the signal with a single string argument onClicked: button.mysignal("test") } diff --git a/src/qml/doc/src/cppintegration/interactqmlfromcpp.qdoc b/src/qml/doc/src/cppintegration/interactqmlfromcpp.qdoc index 9c33979f40..0a824bb5b5 100644 --- a/src/qml/doc/src/cppintegration/interactqmlfromcpp.qdoc +++ b/src/qml/doc/src/cppintegration/interactqmlfromcpp.qdoc @@ -166,9 +166,12 @@ updated, and any \c onButtonTextChanged handlers would not be called. \section2 Invoking QML Methods -All QML methods are exposed to the meta-object system and can be called from C++ -using QMetaObject::invokeMethod(). Method parameters and return values passed -from QML are always translated into QVariant values in C++. +All QML methods are exposed to the meta-object system and can be called from +C++ using QMetaObject::invokeMethod(). You can specify types for the parameters +and the return value after the colon character, as shown in the code snippet +below. This can be useful, for example, when you want to connect a signal in +C++ with a certain signature to a QML-defined method. If you omit the types, +the C++ signature will use QVariant. Here is a C++ application that calls a QML method using QMetaObject::invokeMethod(): @@ -182,9 +185,12 @@ QMetaObject::invokeMethod(): \li \snippet qml/qtbinding/functions-qml/main.cpp 0 \endtable -Notice the Q_RETURN_ARG() and Q_ARG() arguments for QMetaObject::invokeMethod() -must be specified as QVariant types, as this is the generic data type used for -QML method parameters and return values. +Notice the parameter and return type specified after the colon. You can use \l +{QML Basic Types}{basic types} and \l {QML Object Types}{object types} as type +names. + +If the type is omitted in QML, then you must specify QVariant as type with +Q_RETURN_ARG() and Q_ARG() when calling QMetaObject::invokeMethod. \section2 Connecting to QML Signals @@ -210,9 +216,8 @@ QObject::connect(), so that the \c cppSlot() method is called whenever the \snippet qml/qtbinding/signals-qml/main.cpp 0 \endtable -When a QML object type is used as a signal parameter, the parameter should -use \l var as the type, and the value should be received in C++ using the -QVariant type: +A QML object type in a signal parameter is translated to a pointer to the class +in C++: \table \row @@ -226,7 +231,7 @@ QVariant type: id: item width: 100; height: 100 - signal qmlSignal(var anObject) + signal qmlSignal(anObject: Item) MouseArea { anchors.fill: parent @@ -241,18 +246,16 @@ QVariant type: { Q_OBJECT public slots: - void cppSlot(const QVariant &v) { - qDebug() << "Called the C++ slot with value:" << v; + void cppSlot(QQuickItem *item) { + qDebug() << "Called the C++ slot with item:" << item; - QQuickItem *item = - qobject_cast<QQuickItem*>(v.value<QObject*>()); qDebug() << "Item dimensions:" << item->width() << item->height(); } }; int main(int argc, char *argv[]) { - QApplication app(argc, argv); + QGuiApplication app(argc, argv); QQuickView view(QUrl::fromLocalFile("MyItem.qml")); QObject *item = view.rootObject(); diff --git a/src/quick/doc/snippets/qml/qml-extending-types/signals/Button.qml b/src/quick/doc/snippets/qml/qml-extending-types/signals/Button.qml index b40f517234..96f7fc1c7c 100644 --- a/src/quick/doc/snippets/qml/qml-extending-types/signals/Button.qml +++ b/src/quick/doc/snippets/qml/qml-extending-types/signals/Button.qml @@ -55,7 +55,7 @@ Rectangle { id: rect width: 100; height: 100 - signal buttonClicked(int xPos, int yPos) + signal buttonClicked(xPos: int, yPos: int) MouseArea { anchors.fill: parent |