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. Sending signals from CPP to QML using static functions.
Qt 6.11 is out! See what's new in the release blog

Sending signals from CPP to QML using static functions.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 704 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.
  • D Offline
    D Offline
    deleted544
    wrote on last edited by
    #1

    Currently i'm using a libary thats giving me callbacks when the data is ready.

    When the callback function fired it should send a signal to QML so it can change the UI. But its not reacting on my signal that im sending.. When the signal is send from normal timer its working well. But from static functions its just not working.

    For the class im using singleton to access the signals.

    MyClass* MyClass::m_instance = nullptr;
    
    MyClass* MyClass::instance(){
        if (!m_instance) {
            m_instance = new MyClass();
        }
        return m_instance;
    }
    
    void MyClass::callBack(){
         // Do Some calculations
       emit instance()->CallBackEvent();
    }
    

    The header including the signal:

    signals:
        void CallBackEvent();
    

    My QML file looks like:

    Connections {
            target: MyClass
               function onCallBackEvent(){
                console.log("I got called! Change data!!!")
    
            }
        }
    

    When i fire the signal by using a timer it works fine (non static function).

    What might be the problem why my signal is not triggerd?

    J.HilkJ 1 Reply Last reply
    0
    • D deleted544

      Currently i'm using a libary thats giving me callbacks when the data is ready.

      When the callback function fired it should send a signal to QML so it can change the UI. But its not reacting on my signal that im sending.. When the signal is send from normal timer its working well. But from static functions its just not working.

      For the class im using singleton to access the signals.

      MyClass* MyClass::m_instance = nullptr;
      
      MyClass* MyClass::instance(){
          if (!m_instance) {
              m_instance = new MyClass();
          }
          return m_instance;
      }
      
      void MyClass::callBack(){
           // Do Some calculations
         emit instance()->CallBackEvent();
      }
      

      The header including the signal:

      signals:
          void CallBackEvent();
      

      My QML file looks like:

      Connections {
              target: MyClass
                 function onCallBackEvent(){
                  console.log("I got called! Change data!!!")
      
              }
          }
      

      When i fire the signal by using a timer it works fine (non static function).

      What might be the problem why my signal is not triggerd?

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

      @DrWhoozy said in Sending signals from CPP to QML using static functions.:

      What might be the problem why my signal is not triggerd

      static, probably. Connections always relay on concrete object instances -> no static signals -> signals can not be emitted from static functions


      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.

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

        @DrWhoozy said in Sending signals from CPP to QML using static functions.:

        What might be the problem why my signal is not triggerd

        static, probably. Connections always relay on concrete object instances -> no static signals -> signals can not be emitted from static functions

        D Offline
        D Offline
        deleted544
        wrote on last edited by
        #3

        @J-Hilk Thanks for answering. What is your suggestion to send a signal from the callback to QML?

        J.HilkJ 1 Reply Last reply
        0
        • D deleted544

          @J-Hilk Thanks for answering. What is your suggestion to send a signal from the callback to QML?

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

          @DrWhoozy not entirely sure, I think you can bypass this by wrapping the call-back in a lambda ? 🤷‍♂️

          Besides that, see this SO thread I found about Qt Signals and Callbacks:
          https://stackoverflow.com/questions/41771848/qt-emit-signal-from-callback

          It contains two valid solutions, as far as I can tell


          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.

          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