How to integrate qml with c++ when using stackview?
-
wrote on 16 Aug 2023, 15:27 last edited by
There is a
StackView
in my qml scene. In some case, I will push a setting page that contains a bundle ofTextField
or something. I hope that when aTextField
is changed, a signal should be emitted to c++. The main obstacles that I met is the setting page I mentioned will not be created at first, so it is impossible to connect signal at main function. Here is some solutions I came up with, but none of them can meet my requirements.- Emit a signal when setting page is pushed, then c++ connect the corresponding signals. The problem of this solution is that I might have to do many repeated connections.
- When a specific
TextField
is changed, emit a general signal to the stackview, and I just need to connect that signal in main function. The problem of this solution is that I must filter different messages in c++ process funcitons . It also lacks interface extensibility.
Anyone knows a better solution? :)
-
There is a
StackView
in my qml scene. In some case, I will push a setting page that contains a bundle ofTextField
or something. I hope that when aTextField
is changed, a signal should be emitted to c++. The main obstacles that I met is the setting page I mentioned will not be created at first, so it is impossible to connect signal at main function. Here is some solutions I came up with, but none of them can meet my requirements.- Emit a signal when setting page is pushed, then c++ connect the corresponding signals. The problem of this solution is that I might have to do many repeated connections.
- When a specific
TextField
is changed, emit a general signal to the stackview, and I just need to connect that signal in main function. The problem of this solution is that I must filter different messages in c++ process funcitons . It also lacks interface extensibility.
Anyone knows a better solution? :)
wrote on 17 Aug 2023, 09:28 last edited by@Kamichanw I would consider using a context property to communicate from your QML page to the C++ backend, rather than trying to set up signal connections in the C++ code.
-
@Kamichanw I would consider using a context property to communicate from your QML page to the C++ backend, rather than trying to set up signal connections in the C++ code.
@Bob64 context property should be avoided. Prefer Singletons or Creatable types
@Kamichanw can't your QML code call a C++ function instead? It knows what it is interacting with so might as well directly call it. The reverse way (accessing QML objects from c++) should be avoided.
-
@Bob64 context property should be avoided. Prefer Singletons or Creatable types
@Kamichanw can't your QML code call a C++ function instead? It knows what it is interacting with so might as well directly call it. The reverse way (accessing QML objects from c++) should be avoided.
wrote on 17 Aug 2023, 10:40 last edited by@GrecKo I think I had heard that QT is encouraging us to go in that direction but I had been stuck on 5.9.6 until recently and old habits die hard.
Do you have a link to the rationale for preferring singletons? It seems odd in this day and age that singletons should be encouraged!
-
@GrecKo I think I had heard that QT is encouraging us to go in that direction but I had been stuck on 5.9.6 until recently and old habits die hard.
Do you have a link to the rationale for preferring singletons? It seems odd in this day and age that singletons should be encouraged!
@Bob64 context properties are essentially singleton with worth semantics and performance anyway.
QML singletons might not be singleton on the C++ side, it just defines how they are accessed by the QML engine. They provide type safety and tooling support that context properties don't.
-
@Bob64 context properties are essentially singleton with worth semantics and performance anyway.
QML singletons might not be singleton on the C++ side, it just defines how they are accessed by the QML engine. They provide type safety and tooling support that context properties don't.
wrote on 17 Aug 2023, 12:35 last edited by@GrecKo said in How to integrate qml with c++ when using stackview?:
QML singletons might not be singleton on the C++ side
OK, that's a key point for me. I need to look into this in more detail now. Thanks.
-
@Bob64 context properties are essentially singleton with worth semantics and performance anyway.
QML singletons might not be singleton on the C++ side, it just defines how they are accessed by the QML engine. They provide type safety and tooling support that context properties don't.
wrote on 22 Aug 2023, 00:12 last edited by -
wrote on 22 Aug 2023, 01:20 last edited by
Without getting into the discussion about preferred designs, using a relay signal in the top component is easy.
Item { id: root signal textChanged(string text) ChildToBeCreatedLater { TextField { onTextChanged: root.textChanged(text) } } }
-
wrote on 22 Aug 2023, 09:19 last edited by Bob64
@Kamichanw said in How to integrate qml with c++ when using stackview?:
@GrecKo Can you explain why we should prefer Singletons or Creatable and when we use context property?
I think what GrecKo was saying was that context properties should not be used at all now as that is considered to be a deprecated approach. The advice seems to be: in any case where a context property would have seemed most appropriate (i.e., accessing a C++ API from QML via some named access point), use a singleton instead.
Creatable types are for where it makes sense to implement an object in C++ that looks like a component in QML and can be instantiated in your QML code in the same way you can instantiate a
Rectangle
orTimer
or whatever.@Bob64 Can you explain why I should call c++ function rather than emit signals and we I should us signals instead of c++ function directly?
Technically it is possible to connect a QML signal to a C++ slot, but I do not think this method is emphasised in the Qt docs and I have never needed to do it myself. I think the main issue with it is that in order to make the connection, you have to go looking on the C++ side for the QML object. The general advice is that you should try to avoid accessing QML objects from C++. For example, see the warning in the Qt docs about half way down this page: https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html
The more recommended approaches are described in this doc:
-
@Kamichanw said in How to integrate qml with c++ when using stackview?:
@GrecKo Can you explain why we should prefer Singletons or Creatable and when we use context property?
I think what GrecKo was saying was that context properties should not be used at all now as that is considered to be a deprecated approach. The advice seems to be: in any case where a context property would have seemed most appropriate (i.e., accessing a C++ API from QML via some named access point), use a singleton instead.
Creatable types are for where it makes sense to implement an object in C++ that looks like a component in QML and can be instantiated in your QML code in the same way you can instantiate a
Rectangle
orTimer
or whatever.@Bob64 Can you explain why I should call c++ function rather than emit signals and we I should us signals instead of c++ function directly?
Technically it is possible to connect a QML signal to a C++ slot, but I do not think this method is emphasised in the Qt docs and I have never needed to do it myself. I think the main issue with it is that in order to make the connection, you have to go looking on the C++ side for the QML object. The general advice is that you should try to avoid accessing QML objects from C++. For example, see the warning in the Qt docs about half way down this page: https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html
The more recommended approaches are described in this doc:
wrote on 22 Aug 2023, 17:03 last edited by@Bob64 said in How to integrate qml with c++ when using stackview?:
Technically it is possible to connect a QML signal to a C++ slot, but I do not think this method is emphasised in the Qt docs
https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals
The example in the documentation demonstrates connecting to a signal from the root QML item. My example above demonstrates relaying a signal from a non-root, dynamically created item via the root.
-
@Bob64 said in How to integrate qml with c++ when using stackview?:
Technically it is possible to connect a QML signal to a C++ slot, but I do not think this method is emphasised in the Qt docs
https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals
The example in the documentation demonstrates connecting to a signal from the root QML item. My example above demonstrates relaying a signal from a non-root, dynamically created item via the root.
wrote on 23 Aug 2023, 09:38 last edited by@jeremy_k I completely agree that it's possible and I had even linked to the same page as you. I was simply saying that integrating in the C++ to QML direction seems to go against the grain of what Qt themselves recommend, the warning on that page being being a case in point.
6/11