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 Update on Monday, May 27th 2025

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 489 Views
  • 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 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?

    JKSHJ 1 Reply Last reply
    0
    • N Nan Feng

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on 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
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on 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
          JKSHJ 1 Reply Last reply
          2
          • GrecKoG GrecKo

            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
            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on 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

            GrecKoG 1 Reply Last reply
            0
            • JKSHJ JKSH

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

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on 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

              • Login

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