Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to integrate qml with c++ when using stackview?
Forum Updated to NodeBB v4.3 + New Features

How to integrate qml with c++ when using stackview?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
11 Posts 4 Posters 793 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kamichanw
    16 Aug 2023, 15:27

    There is a StackView in my qml scene. In some case, I will push a setting page that contains a bundle of TextField or something. I hope that when a TextField 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.

    1. 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.
    2. 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? :)
    B Offline
    B Offline
    Bob64
    wrote on 17 Aug 2023, 09:28 last edited by
    #2

    @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.

    G 1 Reply Last reply 17 Aug 2023, 10:01
    0
    • B Bob64
      17 Aug 2023, 09:28

      @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.

      G Offline
      G Offline
      GrecKo
      Qt Champions 2018
      wrote on 17 Aug 2023, 10:01 last edited by
      #3

      @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.

      B 1 Reply Last reply 17 Aug 2023, 10:40
      1
      • G GrecKo
        17 Aug 2023, 10:01

        @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.

        B Offline
        B Offline
        Bob64
        wrote on 17 Aug 2023, 10:40 last edited by
        #4

        @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!

        G 1 Reply Last reply 17 Aug 2023, 12:13
        0
        • B Bob64
          17 Aug 2023, 10:40

          @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!

          G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 17 Aug 2023, 12:13 last edited by
          #5

          @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.

          B K 2 Replies Last reply 17 Aug 2023, 12:35
          2
          • G GrecKo
            17 Aug 2023, 12:13

            @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.

            B Offline
            B Offline
            Bob64
            wrote on 17 Aug 2023, 12:35 last edited by
            #6

            @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.

            1 Reply Last reply
            0
            • G GrecKo
              17 Aug 2023, 12:13

              @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.

              K Offline
              K Offline
              Kamichanw
              wrote on 22 Aug 2023, 00:12 last edited by
              #7

              @GrecKo Can you explain why we should prefer Singletons or Creatable and when we use context property?

              @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?

              B 1 Reply Last reply 22 Aug 2023, 09:19
              0
              • J Offline
                J Offline
                jeremy_k
                wrote on 22 Aug 2023, 01:20 last edited by
                #8

                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)
                        }
                    }
                }
                

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • K Kamichanw
                  22 Aug 2023, 00:12

                  @GrecKo Can you explain why we should prefer Singletons or Creatable and when we use context property?

                  @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?

                  B Offline
                  B Offline
                  Bob64
                  wrote on 22 Aug 2023, 09:19 last edited by Bob64
                  #9

                  @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 or Timer 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:

                  https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html

                  J 1 Reply Last reply 22 Aug 2023, 17:03
                  0
                  • B Bob64
                    22 Aug 2023, 09:19

                    @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 or Timer 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:

                    https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html

                    J Offline
                    J Offline
                    jeremy_k
                    wrote on 22 Aug 2023, 17:03 last edited by
                    #10

                    @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.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    B 1 Reply Last reply 23 Aug 2023, 09:38
                    0
                    • J jeremy_k
                      22 Aug 2023, 17:03

                      @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.

                      B Offline
                      B Offline
                      Bob64
                      wrote on 23 Aug 2023, 09:38 last edited by
                      #11

                      @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.

                      1 Reply Last reply
                      0

                      11/11

                      23 Aug 2023, 09:38

                      • Login

                      • Login or register to search.
                      11 out of 11
                      • First post
                        11/11
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved