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. Automatic connection of QML signal and C++ slot?

Automatic connection of QML signal and C++ slot?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
16 Posts 6 Posters 1.4k Views 2 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by Tom asso
    #1

    Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
    Thanks!

    Axel SpoerlA J.HilkJ 2 Replies Last reply
    0
    • Tom assoT Tom asso

      @GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #14

      @Tom-asso said in Automatic connection of QML signal and C++ slot?:

      But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?

      It’s better to call a C++ function from QML and do the plumbing in C++, I wouldn’t know of a use case where such connections are the best possible solution.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1
      • Tom assoT Tom asso

        Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
        Thanks!

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        @Tom-asso
        Yes in the sense that every binding is an automatic signal/slot connection.
        No in the sense, that functional slots have to be explicitly connected. The syntax is quite simple, so I wouldn’t even call that a downside.

        Software Engineer
        The Qt Company, Oslo

        Tom assoT 1 Reply Last reply
        1
        • Axel SpoerlA Axel Spoerl

          @Tom-asso
          Yes in the sense that every binding is an automatic signal/slot connection.
          No in the sense, that functional slots have to be explicitly connected. The syntax is quite simple, so I wouldn’t even call that a downside.

          Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by
          #3

          @Axel-Spoerl Design question for you:
          If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?

          1. declare/define 10 slot functions
          2. make 10 calls to connect(), once for each slot declared in step 1

          That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
          Do qtcreator or qtdesignstudio offer a more 'automatic' way?
          Thanks!

          GrecKoG Axel SpoerlA 2 Replies Last reply
          0
          • Tom assoT Tom asso

            @Axel-Spoerl Design question for you:
            If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?

            1. declare/define 10 slot functions
            2. make 10 calls to connect(), once for each slot declared in step 1

            That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
            Do qtcreator or qtdesignstudio offer a more 'automatic' way?
            Thanks!

            GrecKoG Online
            GrecKoG Online
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #4

            @Tom-asso you usually don't connect to QML signals in c++. What you should do is expose your c++ slots and type/objects and call them in QML.

            onClicked: myCppObject.doStuff()

            1 Reply Last reply
            1
            • Tom assoT Tom asso

              @Axel-Spoerl Design question for you:
              If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?

              1. declare/define 10 slot functions
              2. make 10 calls to connect(), once for each slot declared in step 1

              That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
              Do qtcreator or qtdesignstudio offer a more 'automatic' way?
              Thanks!

              Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #5

              @Tom-asso said in Automatic connection of QML signal and C++ slot?:

              If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?

              1. declare/define 10 slot functions
              2. make 10 calls to connect(), once for each slot declared in step 1

              That's the gist of it, right?

              Yes.

              Those 10 connect() calls just seem like a lot

              yy9pis the sequence in vim, doing 80% of this job for you. The rest is usually about editing a few letters :-)

              You can of course use autoconnect - totally fine, it has been designed for the comfort you are seeking.
              The caveat is, that a mismatch of a single character will leave signal/slot unconnected without notice.
              Depending on an exact string match in an XML file and C++ code, comes particularly unhandy when searching for a bug. I have seen furious forum posts and bugreports about QObject::connect()allegedly not working properly. I remember a project from a few years ago, where a company's quality department has done a huge effort in renaming_all_underline_stuff intoCamelStyle, autoconnected slots included. Compiled like a charm, and set the UI on fire.

              Software Engineer
              The Qt Company, Oslo

              Tom assoT 1 Reply Last reply
              1
              • Tom assoT Tom asso

                Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
                Thanks!

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @Tom-asso don't use that feature if you can help yourself!

                These days there's Ai/copilot to do repetitive boring and simple tasks. Like creating slots for signals or unit tests for functions.

                Rather use that.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                JonBJ 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Tom-asso don't use that feature if you can help yourself!

                  These days there's Ai/copilot to do repetitive boring and simple tasks. Like creating slots for signals or unit tests for functions.

                  Rather use that.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @J-Hilk said in Automatic connection of QML signal and C++ slot?:

                  These days there's Ai/copilot to do repetitive boring and simple tasks.

                  Nothing will ever equate to the efficiency and clarity of @Axel-Spoerl's yy9p in vim ;-)

                  J.HilkJ 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @J-Hilk said in Automatic connection of QML signal and C++ slot?:

                    These days there's Ai/copilot to do repetitive boring and simple tasks.

                    Nothing will ever equate to the efficiency and clarity of @Axel-Spoerl's yy9p in vim ;-)

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #8

                    @JonB I actually have no idea what it is :P first time I heard about it.

                    But just wait until Ai is part of your OS and always listening, like Alexa....

                    Not a disturbing thought at all.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    JonBJ 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @JonB I actually have no idea what it is :P first time I heard about it.

                      But just wait until Ai is part of your OS and always listening, like Alexa....

                      Not a disturbing thought at all.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @J-Hilk If you don't know what yy9p does in vi your life is lacking. Surely it's clear what it does anyway... ;)

                      J.HilkJ GrecKoG 2 Replies Last reply
                      0
                      • JonBJ JonB

                        @J-Hilk If you don't know what yy9p does in vi your life is lacking. Surely it's clear what it does anyway... ;)

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #10

                        @JonB so I asked chatgpt :P so I now know!

                        and I also generally try not to use a cmd line text editor if I can help it ;)


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        S 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @J-Hilk If you don't know what yy9p does in vi your life is lacking. Surely it's clear what it does anyway... ;)

                          GrecKoG Online
                          GrecKoG Online
                          GrecKo
                          Qt Champions 2018
                          wrote on last edited by GrecKo
                          #11

                          I'm sorry but do not connect to QML signals in c++, even though it's simple with some editor magic (Qt Creator ctrl-d is much easier to grok than vim bindings btw). You will still be left with verbose and subpar code.

                          https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                          https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
                          https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                          https://youtu.be/vzs5VPTf4QQ?t=23m20s

                          Tom assoT 1 Reply Last reply
                          3
                          • Axel SpoerlA Axel Spoerl

                            @Tom-asso said in Automatic connection of QML signal and C++ slot?:

                            If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?

                            1. declare/define 10 slot functions
                            2. make 10 calls to connect(), once for each slot declared in step 1

                            That's the gist of it, right?

                            Yes.

                            Those 10 connect() calls just seem like a lot

                            yy9pis the sequence in vim, doing 80% of this job for you. The rest is usually about editing a few letters :-)

                            You can of course use autoconnect - totally fine, it has been designed for the comfort you are seeking.
                            The caveat is, that a mismatch of a single character will leave signal/slot unconnected without notice.
                            Depending on an exact string match in an XML file and C++ code, comes particularly unhandy when searching for a bug. I have seen furious forum posts and bugreports about QObject::connect()allegedly not working properly. I remember a project from a few years ago, where a company's quality department has done a huge effort in renaming_all_underline_stuff intoCamelStyle, autoconnected slots included. Compiled like a charm, and set the UI on fire.

                            Tom assoT Offline
                            Tom assoT Offline
                            Tom asso
                            wrote on last edited by
                            #12

                            @Axel-Spoerl autoconnect is only for Qt-Widgets, not enabled for QtQuick/QML, correct?

                            1 Reply Last reply
                            0
                            • GrecKoG GrecKo

                              I'm sorry but do not connect to QML signals in c++, even though it's simple with some editor magic (Qt Creator ctrl-d is much easier to grok than vim bindings btw). You will still be left with verbose and subpar code.

                              https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                              https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
                              https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                              https://youtu.be/vzs5VPTf4QQ?t=23m20s

                              Tom assoT Offline
                              Tom assoT Offline
                              Tom asso
                              wrote on last edited by Tom asso
                              #13

                              @GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?

                              Axel SpoerlA 1 Reply Last reply
                              0
                              • Tom assoT Tom asso

                                @GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?

                                Axel SpoerlA Offline
                                Axel SpoerlA Offline
                                Axel Spoerl
                                Moderators
                                wrote on last edited by
                                #14

                                @Tom-asso said in Automatic connection of QML signal and C++ slot?:

                                But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?

                                It’s better to call a C++ function from QML and do the plumbing in C++, I wouldn’t know of a use case where such connections are the best possible solution.

                                Software Engineer
                                The Qt Company, Oslo

                                1 Reply Last reply
                                1
                                • J.HilkJ J.Hilk

                                  @JonB so I asked chatgpt :P so I now know!

                                  and I also generally try not to use a cmd line text editor if I can help it ;)

                                  S Offline
                                  S Offline
                                  SimonSchroeder
                                  wrote on last edited by
                                  #15

                                  @J-Hilk said in Automatic connection of QML signal and C++ slot?:

                                  I also generally try not to use a cmd line text editor if I can help it ;)

                                  There also graphical versions of vi. And Qt Creator supports FakeVim which you can quickly toggle.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • S SimonSchroeder

                                    @J-Hilk said in Automatic connection of QML signal and C++ slot?:

                                    I also generally try not to use a cmd line text editor if I can help it ;)

                                    There also graphical versions of vi. And Qt Creator supports FakeVim which you can quickly toggle.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #16

                                    @SimonSchroeder I think @J-Hilk's interest in using vi/vim in any shape or form is absolute 0! We were jesting back & forth.

                                    1 Reply Last reply
                                    0
                                    • Tom assoT Tom asso has marked this topic as solved on

                                    • Login

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