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. Can the signal slot connection method in Cpp && Qml be used in Qt5?
Forum Updated to NodeBB v4.3 + New Features

Can the signal slot connection method in Cpp && Qml be used in Qt5?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 516 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.
  • N Offline
    N Offline
    Nan Feng
    wrote on 20 Oct 2021, 02:33 last edited by
    #1

    Hi, in the connection of signals and slots in Cpp && Qml (Qml emits signals and connects to the slot function of a class in C++), I found many ways to do this: QObject:connect(item, SIGNAL(.. ), &MyClass, SLOT(...)), many books have this kind of Qt4 syntax. Can't I use Qt5's new syntax?

    J 1 Reply Last reply 20 Oct 2021, 02:42
    0
    • N Nan Feng
      20 Oct 2021, 02:33

      Hi, in the connection of signals and slots in Cpp && Qml (Qml emits signals and connects to the slot function of a class in C++), I found many ways to do this: QObject:connect(item, SIGNAL(.. ), &MyClass, SLOT(...)), many books have this kind of Qt4 syntax. Can't I use Qt5's new syntax?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 20 Oct 2021, 02:42 last edited by
      #2

      @Nan-Feng said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:

      Can't I use Qt5's new syntax?

      Unfortunately not. Qt 5's new syntax only works because the C++ compiler can check both the signal and the slot. However, the C++ compiler cannot see QML signals or QML slots.

      The old (Qt 4) works here because the checks are done at runtime, using string comparisons.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      5
      • F Offline
        F Offline
        fcarney
        wrote on 20 Oct 2021, 14:54 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 21 Oct 2021, 09:11 last edited by
          #4

          Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.

          Some links explaining why:

          • https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
          • http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
          • https://youtu.be/vzs5VPTf4QQ?t=23m20s
          J 1 Reply Last reply 21 Oct 2021, 12:14
          2
          • G GrecKo
            21 Oct 2021, 09:11

            Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.

            Some links explaining why:

            • https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
            • http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
            • https://youtu.be/vzs5VPTf4QQ?t=23m20s
            J Offline
            J Offline
            JKSH
            Moderators
            wrote on 21 Oct 2021, 12:14 last edited by JKSH
            #5

            @GrecKo said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:

            Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.

            Some links explaining why:

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

            The links encourage developers to avoid calling QML object methods directly from C++. Using signal-slot connections are quite different from the calling methods directly, since they still allow good decoupling. The same idea applies to pure C++ code -- If the sender object does not know anything about the receiver object, then decoupling has been achieved.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            G 1 Reply Last reply 21 Oct 2021, 12:35
            0
            • J JKSH
              21 Oct 2021, 12:14

              @GrecKo said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:

              Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.

              Some links explaining why:

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

              The links encourage developers to avoid calling QML object methods directly from C++. Using signal-slot connections are quite different from the calling methods directly, since they still allow good decoupling. The same idea applies to pure C++ code -- If the sender object does not know anything about the receiver object, then decoupling has been achieved.

              G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 21 Oct 2021, 12:35 last edited by
              #6

              @JKSH No it doesn't really allow good decoupling, to connect to a QML object from C++, you have to be aware of it.

              The example in the first link is specifically about connecting a QML signal to a c++ slot.

              If your C++ object is a helper for QML to which you pass the QML object reference from QML and is not related to your business logic it's fine.

              Using findChild or traversing the QML object hierarchy from c++ is almost always not fine.

              1 Reply Last reply
              0

              1/6

              20 Oct 2021, 02:33

              • Login

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