diff --git a/cpp-lib/readme.md b/cpp-lib/readme.md index 36341f1..416fd21 100644 --- a/cpp-lib/readme.md +++ b/cpp-lib/readme.md @@ -13,7 +13,7 @@ conversions, as long as you only use `bool`, `long`, `double`, `QString`, `QByteArray` or (nested) `QVariant` lists of mentioned types. For the complete list of marshalled types please see `toQVariant()` and -`from_qvariant` in [marshal.cpp](../src/cpp/marshal.cpp). +`from_qvariant()` in [marshal.cpp](../src/cpp/marshal.cpp). diff --git a/doc/help.htm b/doc/help.htm index e1d996d..dfa540e 100644 --- a/doc/help.htm +++ b/doc/help.htm @@ -44,9 +44,9 @@ find-quick-item (object-name) - Finds the first QQuickItem matching OBJECT-NAME. Locally set *ROOT-ITEM* if - you want to find items inside a specific item, like in a QML Repeater. See - also note in sources. + Finds the first QQuickItem matching OBJECT-NAME. + See also WITH-ROOT-ITEM if you want to find items inside a specific item, + like in a QML Repeater. pixel-ratio () @@ -294,6 +294,17 @@ (populate-item-model))) +with-root-item (root-item) + + Say you have a Repeater QML item with multiple instances of the same + QQuickItem. The children of those QQuickItems all have the same object names, + respectively. In order to access those child items, we need to search in one + specific item of the Repeater. + + (with-root-item (q! |itemAt| ui:*repeater* 0) + (q< |text| ui:*edit*)) + + diff --git a/src/lisp/package.lisp b/src/lisp/package.lisp index 1298f08..9a78927 100644 --- a/src/lisp/package.lisp +++ b/src/lisp/package.lisp @@ -58,6 +58,7 @@ #:set-clipboard-text #:tr #:view-status-changed + #:with-root-item #:!)) (defpackage :qml-user diff --git a/src/lisp/qml.lisp b/src/lisp/qml.lisp index 7717e83..e28801f 100644 --- a/src/lisp/qml.lisp +++ b/src/lisp/qml.lisp @@ -29,35 +29,29 @@ (defun find-quick-item (object-name) "args: (object-name) - Finds the first QQuickItem matching OBJECT-NAME. Locally set *ROOT-ITEM* if - you want to find items inside a specific item, like in a QML Repeater. See - also note in sources." - ;; - ;; When to use *ROOT-ITEM* - ;; - ;; Say you have a Repeater QML item with multiple instances of the same - ;; QQuickItem. The children of those QQuickItems all have the same object - ;; names, respectively. In order to access those child items, we need to - ;; search in the specific item of the Repeater. - ;; - ;; So, we locally set (not bind, see note N.B.) *ROOT-ITEM* in order to find - ;; a specific child item inside the Repeater: - ;; - ;; (setf qml:*root-item* (q! |itemAt| ui:*repeater* 0)) ; (1) set - ;; ;; everything we do here will only affect children of the first - ;; ;; item in ui:*repeater* (see index 0 above) - ;; (q< |text| ui:*edit*) - ;; (setf qml:*root-item* nil) ; (2) reset - ;; - ;; N.B. we need SETF (instead of LET) because of the global var and threads - ;; (QRUN* is used internally here) - ;; + Finds the first QQuickItem matching OBJECT-NAME. + See also WITH-ROOT-ITEM if you want to find items inside a specific item, + like in a QML Repeater." (let ((parent (or *root-item* (root-item)))) (when (and parent (/= 0 (qt-object-address parent))) (if (string= (qobject-name parent) object-name) parent (qfind-child parent object-name))))) +(defmacro with-root-item (root-item &body body) + "args: (root-item) + Say you have a Repeater QML item with multiple instances of the same + QQuickItem. The children of those QQuickItems all have the same object names, + respectively. In order to access those child items, we need to search in one + specific item of the Repeater. + (with-root-item (q! |itemAt| ui:*repeater* 0) + (q< |text| ui:*edit*))" + `(prog2 + (setf qml:*root-item* ,root-item) + (progn + ,@body) + (setf qml:*root-item* nil))) + (defun quick-item (item/name) ;; for internal use (cond ((stringp item/name)