Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Couldn't catch signal from Cpp in QML
Forum Updated to NodeBB v4.3 + New Features

Couldn't catch signal from Cpp in QML

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 302 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.
  • J Offline
    J Offline
    johndummy
    wrote on last edited by johndummy
    #1

    I'm trying to transfer some QString from cpp to qml. Here are the steps I've done so far.

    1. Created a class inherited from QObject and defined signal in it.
    class exampleClass: public QObject {
    
        Q_OBJECT
    .
    .
    signals:
    
    void mySignal (QString myStr);
    
    }
    
    1. Somewhere in the class implementation, i emit the signal i.e.
    void exampleClass::exampleFtn( . . . ){
    .
    .
    qDebug("Before transmitting Signal");        // This gets triggered
    emit mySignal ("Hello from CPP");
    
    }
    
    1. In my main.cpp, I've initialized the class and expose it to QML i.e.
    int main(...) {
    
    .
    .
    exampleClass *exampleObj= new exampleClass(&app);
    Engine.rootContext()->setContextProperty("exampleObj",  exampleObj);
    }
    
    1. And finally, in QML i have connected the signal i.e.
    Window {
    
    .
    .
    Connections {
            target: exampleObj
            function mySignal(myStr) {
                console.log("signal from C++ recieved !")    // Doesn't get triggered
                console.log(myStr)                                           //  Doesn't get triggered
            }
        }
    
    sierdzioS 1 Reply Last reply
    0
    • J johndummy

      I'm trying to transfer some QString from cpp to qml. Here are the steps I've done so far.

      1. Created a class inherited from QObject and defined signal in it.
      class exampleClass: public QObject {
      
          Q_OBJECT
      .
      .
      signals:
      
      void mySignal (QString myStr);
      
      }
      
      1. Somewhere in the class implementation, i emit the signal i.e.
      void exampleClass::exampleFtn( . . . ){
      .
      .
      qDebug("Before transmitting Signal");        // This gets triggered
      emit mySignal ("Hello from CPP");
      
      }
      
      1. In my main.cpp, I've initialized the class and expose it to QML i.e.
      int main(...) {
      
      .
      .
      exampleClass *exampleObj= new exampleClass(&app);
      Engine.rootContext()->setContextProperty("exampleObj",  exampleObj);
      }
      
      1. And finally, in QML i have connected the signal i.e.
      Window {
      
      .
      .
      Connections {
              target: exampleObj
              function mySignal(myStr) {
                  console.log("signal from C++ recieved !")    // Doesn't get triggered
                  console.log(myStr)                                           //  Doesn't get triggered
              }
          }
      
      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @johndummy you've got it almost correct. The only error I see is that in QML, inside of Connections, you should make the function name match your signal. So:

      Connections {
              target: exampleObj
              function mySignal(myStr) {
                  console.log("signal from C++ recieved !")    // Doesn't get triggered
                  console.log(myStr)                                           //  Doesn't get triggered
              }
          }
      

      (Z(:^

      J 2 Replies Last reply
      0
      • sierdzioS sierdzio

        @johndummy you've got it almost correct. The only error I see is that in QML, inside of Connections, you should make the function name match your signal. So:

        Connections {
                target: exampleObj
                function mySignal(myStr) {
                    console.log("signal from C++ recieved !")    // Doesn't get triggered
                    console.log(myStr)                                           //  Doesn't get triggered
                }
            }
        
        J Offline
        J Offline
        johndummy
        wrote on last edited by johndummy
        #3

        @sierdzio : That's a typo error in the post. sorry for that.

        My function name match exactly with my signal. The qml doesn't give me runtime error or warnings. I still don't get why i'm not able to capture the signal.

        P.S: I will edit my initial post to correct the typo (if you don't mind).

        1 Reply Last reply
        1
        • sierdzioS sierdzio

          @johndummy you've got it almost correct. The only error I see is that in QML, inside of Connections, you should make the function name match your signal. So:

          Connections {
                  target: exampleObj
                  function mySignal(myStr) {
                      console.log("signal from C++ recieved !")    // Doesn't get triggered
                      console.log(myStr)                                           //  Doesn't get triggered
                  }
              }
          
          J Offline
          J Offline
          johndummy
          wrote on last edited by
          #4

          I changed mySignal to onMySignal and it worked now.

              function onMySignal(myStr) {
                  console.log("signal from C++ recieved !")  
                  console.log(myStr)                                           
              }
          

          Even onmySignal doesn't work. One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter.

          Christian EhrlicherC 1 Reply Last reply
          0
          • J johndummy has marked this topic as solved on
          • J johndummy

            I changed mySignal to onMySignal and it worked now.

                function onMySignal(myStr) {
                    console.log("signal from C++ recieved !")  
                    console.log(myStr)                                           
                }
            

            Even onmySignal doesn't work. One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            @johndummy said in Couldn't catch signal from Cpp in QML:

            One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter

            Not that this is documented somewhere.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1

            • Login

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