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. How to catch the signal emitted from C++ global function in QML
Forum Updated to NodeBB v4.3 + New Features

How to catch the signal emitted from C++ global function in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
22 Posts 3 Posters 8.0k Views 3 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.
  • Naveen_DN Offline
    Naveen_DN Offline
    Naveen_D
    wrote on last edited by Naveen_D
    #7

    only using the global variable, is it possible to make signal slot communication from cpp to Qml?

    Naveen_D

    1 Reply Last reply
    0
    • sierdzioS sierdzio

      Now you have 3 separate instances of Globaltest, and you want a single one :-)

      Try this:

      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          Globaltest testobj;
      
          QQmlApplicationEngine engine;
          engine.rootContext()->setContextProperty("TestObject",&testobj);
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          testobj.test(); // This emits the signal. Not the nicest solution, but should work for now.
      
          return app.exec();
      }
      
      // QML
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Connections {
            target: TestObject
            onTestSignal: console.log("Signal caught")
         }
      
          MouseArea{
              id: mouse
              anchors.fill: parent
              onClicked: {
                  TestObject.test() // Emits from QML
              }
          }
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #8

      @Naveen_D
      @sierdzio already showed you how to

      Connections {
      target: TestObject
      onTestSignal: console.log("Signal caught")
      }

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Naveen_DN 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Naveen_D
        @sierdzio already showed you how to

        Connections {
        target: TestObject
        onTestSignal: console.log("Signal caught")
        }

        Naveen_DN Offline
        Naveen_DN Offline
        Naveen_D
        wrote on last edited by
        #9

        @raven-worx i tried as said by @sierdzio but i am not able to catch the signal.
        here is the code.
        .cpp

        #include "globaltest.h"
        
        Globaltest testobj;
        Globaltest::Globaltest(QObject *parent) : QObject(parent)
        {
        }
        
        void Globaltest::test()
        {
            qDebug()<<"test called"<<endl;
            output();
        }
        
        void output()
        {
            qDebug()<<"output function called"<<endl;
            testobj.testSignal();
        }
        
        

        main.cpp

        #include <QGuiApplication>
        #include <QApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        
        #include "globaltest.h"
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
            Globaltest testobj;
        
            qmlRegisterType<Globaltest>("com.globalCpp",1,0,"Globaltest");
        
        
            QQmlApplicationEngine engine;
            engine.rootContext()->setContextProperty("TestObject",&testobj);
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        
            return app.exec();
        }
        
        

        main.qml

        import QtQuick 2.7
        import QtQuick.Window 2.2
        import com.globalCpp 1.0
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
            Connections {
                  target: TestObject
                  onTestSignal: console.log("Signal caught")
               }
            MouseArea{
                    id: mouse
                    anchors.fill: parent
                    onClicked: {
                        TestObject.test()
                    }
                }
        }
        

        Naveen_D

        raven-worxR 1 Reply Last reply
        0
        • Naveen_DN Naveen_D

          @raven-worx i tried as said by @sierdzio but i am not able to catch the signal.
          here is the code.
          .cpp

          #include "globaltest.h"
          
          Globaltest testobj;
          Globaltest::Globaltest(QObject *parent) : QObject(parent)
          {
          }
          
          void Globaltest::test()
          {
              qDebug()<<"test called"<<endl;
              output();
          }
          
          void output()
          {
              qDebug()<<"output function called"<<endl;
              testobj.testSignal();
          }
          
          

          main.cpp

          #include <QGuiApplication>
          #include <QApplication>
          #include <QQmlApplicationEngine>
          #include <QQmlContext>
          
          #include "globaltest.h"
          
          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
              Globaltest testobj;
          
              qmlRegisterType<Globaltest>("com.globalCpp",1,0,"Globaltest");
          
          
              QQmlApplicationEngine engine;
              engine.rootContext()->setContextProperty("TestObject",&testobj);
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          
              return app.exec();
          }
          
          

          main.qml

          import QtQuick 2.7
          import QtQuick.Window 2.2
          import com.globalCpp 1.0
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              Connections {
                    target: TestObject
                    onTestSignal: console.log("Signal caught")
                 }
              MouseArea{
                      id: mouse
                      anchors.fill: parent
                      onClicked: {
                          TestObject.test()
                      }
                  }
          }
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #10

          @Naveen_D
          you are creating two different instances of your your object. One in the main() and one in the cpp file.
          You need to ensure that you operate on the same instance. Take a look at the Singleton pattern:

          class Globaltest {
          public:
              static Globaltest* Globaltest::Instance() {  //static method
                    static Globaltest* instance = new Globaltest;
                    return instance;
              }
          private:
               Globaltest();  //hidden constructor --> only allow to use our static Instance() method
          };
          
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Naveen_DN 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Naveen_D
            you are creating two different instances of your your object. One in the main() and one in the cpp file.
            You need to ensure that you operate on the same instance. Take a look at the Singleton pattern:

            class Globaltest {
            public:
                static Globaltest* Globaltest::Instance() {  //static method
                      static Globaltest* instance = new Globaltest;
                      return instance;
                }
            private:
                 Globaltest();  //hidden constructor --> only allow to use our static Instance() method
            };
            
            
            Naveen_DN Offline
            Naveen_DN Offline
            Naveen_D
            wrote on last edited by
            #11

            @raven-worx without using singleton...it is not possible? becoz i have a global object of that class...

            Naveen_D

            raven-worxR 1 Reply Last reply
            0
            • Naveen_DN Naveen_D

              @raven-worx without using singleton...it is not possible? becoz i have a global object of that class...

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #12

              @Naveen_D
              sure, but you need to make sure you are using the right object instance. The singleton is an easy and understandable mechanism, which prevents some possible poitfalls.
              As i said in your posted example you are using 2 different instances. Define the global object as global static object in the header file instead of the source file.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              Naveen_DN 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Naveen_D
                sure, but you need to make sure you are using the right object instance. The singleton is an easy and understandable mechanism, which prevents some possible poitfalls.
                As i said in your posted example you are using 2 different instances. Define the global object as global static object in the header file instead of the source file.

                Naveen_DN Offline
                Naveen_DN Offline
                Naveen_D
                wrote on last edited by
                #13

                @raven-worx
                if i define the global object as global static object in the header file instead of the source file. it is not necessary to again create an object in main.cpp and use context property?..without this i can directly catch the signal using signal handler in qml ?

                Naveen_D

                raven-worxR 1 Reply Last reply
                0
                • Naveen_DN Naveen_D

                  @raven-worx
                  if i define the global object as global static object in the header file instead of the source file. it is not necessary to again create an object in main.cpp and use context property?..without this i can directly catch the signal using signal handler in qml ?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #14

                  @Naveen_D said in How to catch the signal emitted from C++ global function in QML:

                  it is not necessary to again create an object in main.cpp and use context property?

                  the context property of course is still necessary. But not the duplicate object creation. Instead reference the static global one from the header file wherever you need it.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  Naveen_DN 2 Replies Last reply
                  1
                  • raven-worxR raven-worx

                    @Naveen_D said in How to catch the signal emitted from C++ global function in QML:

                    it is not necessary to again create an object in main.cpp and use context property?

                    the context property of course is still necessary. But not the duplicate object creation. Instead reference the static global one from the header file wherever you need it.

                    Naveen_DN Offline
                    Naveen_DN Offline
                    Naveen_D
                    wrote on last edited by
                    #15

                    @raven-worx i want to use this global obj in global function to emit the signal from that function.
                    the way i am declaring the global function is right ?

                    #ifndef GLOBALTEST_H
                    #define GLOBALTEST_H
                    
                    #include <QObject>
                    #include <QDebug>
                    
                    void output();
                    class Globaltest : public QObject
                    {
                        Q_OBJECT
                        static Globaltest *s_instance;
                    public:
                        explicit Globaltest(QObject *parent = 0);
                    
                    signals:
                        void testSignal();
                    
                    
                    public slots:
                        Q_INVOKABLE void test();
                    };
                    
                    #endif // GLOBALTEST_H
                    

                    Naveen_D

                    1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @Naveen_D said in How to catch the signal emitted from C++ global function in QML:

                      it is not necessary to again create an object in main.cpp and use context property?

                      the context property of course is still necessary. But not the duplicate object creation. Instead reference the static global one from the header file wherever you need it.

                      Naveen_DN Offline
                      Naveen_DN Offline
                      Naveen_D
                      wrote on last edited by
                      #16

                      Hi, as you said i have only one instance of the object in the below code but when i use this object in set context property i am getting QVariant error...the error is

                      /home/ubuntu/Qt5.7.0/5.7/gcc_64/include/QtCore/qvariant.h:471: error: 'QVariant::QVariant(void)' is private
                      inline QVariant(void ) Q_DECL_EQ_DELETE;

                      and if i remove the '&' and run the code i get this qml error
                      QML debugging is enabled. Only use this in a safe environment.
                      qrc:/main.qml:20: TypeError: Cannot call method 'test' of null

                      can anyone tell what is wrong in the code and what i need to change
                      Thanks

                      .h

                      #ifndef GLOBALTEST_H
                      #define GLOBALTEST_H
                      
                      #include <QObject>
                      #include <QDebug>
                      
                      void output();
                      class Globaltest : public QObject
                      {
                          Q_OBJECT
                      
                      public:
                          explicit Globaltest(QObject *parent = 0);
                      
                      signals:
                          void testSignal();
                      
                      
                      public slots:
                          Q_INVOKABLE void test();
                      };
                      extern Globaltest *MySender;
                      #endif // GLOBALTEST_H
                      
                      

                      .cpp

                      #include "globaltest.h"
                      
                      Globaltest *MySender;
                      Globaltest::Globaltest(QObject *parent) : QObject(parent)
                      {
                      }
                      
                      //Globaltest *Globaltest::s_instance = 0;
                      
                      void Globaltest::test()
                      {
                          qDebug()<<"test called"<<endl;
                          output();
                      }
                      
                      void output()
                      {
                          qDebug()<<"output function called"<<endl;
                          MySender= new Globaltest;
                          MySender->testSignal();
                      }
                      
                      

                      main.cpp

                      #include <QGuiApplication>
                      #include <QApplication>
                      #include <QQmlApplicationEngine>
                      #include <QQmlContext>
                      
                      #include "globaltest.h"
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication app(argc, argv);
                      
                          qmlRegisterType<Globaltest>("com.globalCpp",1,0,"Globaltest");
                      
                          QQmlApplicationEngine engine;
                          engine.rootContext()->setContextProperty("TestObject",&MySender); //Getting error here //
                          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                      
                          return app.exec();
                      }
                      
                      

                      main.qml

                      import QtQuick 2.7
                      import QtQuick.Window 2.2
                      import com.globalCpp 1.0
                      
                      Window {
                          visible: true
                          width: 640
                          height: 480
                          title: qsTr("Hello World")
                      
                          Connections {
                                target: TestObject
                                onTestSignal: console.log("Signal caught")
                             }
                      
                          MouseArea{
                                  id: mouse
                                  anchors.fill: parent
                                  onClicked: {
                                      TestObject.test()
                                  }
                              }
                      }
                      
                      

                      Naveen_D

                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #17

                        You are declaring MySender to be a pointer, so using '&' in setContextProperty() is wrong. '&' is a way to extract a pointer from non-pointer variable. Here you already have a pointer, so no need for '&'.

                        You declare MySender variable, but you never set it to any value (meaning: you never create an object of type Globaltest). So QML engine - rightfully - complains that the object is null (== not set).

                        You need to set the value of MySender somewhere in your code (before call to setContextProperty), in other words:

                        MySender = new GlobalTest;
                        

                        (Z(:^

                        Naveen_DN raven-worxR 2 Replies Last reply
                        0
                        • sierdzioS sierdzio

                          You are declaring MySender to be a pointer, so using '&' in setContextProperty() is wrong. '&' is a way to extract a pointer from non-pointer variable. Here you already have a pointer, so no need for '&'.

                          You declare MySender variable, but you never set it to any value (meaning: you never create an object of type Globaltest). So QML engine - rightfully - complains that the object is null (== not set).

                          You need to set the value of MySender somewhere in your code (before call to setContextProperty), in other words:

                          MySender = new GlobalTest;
                          
                          Naveen_DN Offline
                          Naveen_DN Offline
                          Naveen_D
                          wrote on last edited by
                          #18

                          @sierdzio ya that i came to know since i am using pointer var no need of '&' but in cpp before emitting a signal i am allocating memory for that global object...

                          Naveen_D

                          1 Reply Last reply
                          0
                          • sierdzioS sierdzio

                            You are declaring MySender to be a pointer, so using '&' in setContextProperty() is wrong. '&' is a way to extract a pointer from non-pointer variable. Here you already have a pointer, so no need for '&'.

                            You declare MySender variable, but you never set it to any value (meaning: you never create an object of type Globaltest). So QML engine - rightfully - complains that the object is null (== not set).

                            You need to set the value of MySender somewhere in your code (before call to setContextProperty), in other words:

                            MySender = new GlobalTest;
                            
                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #19

                            @Naveen_D
                            to add up to @sierdzio
                            also remove the line MySender= new Globaltest; from your output() method.
                            Instead call it once before you set it as context property.

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            sierdzioS 1 Reply Last reply
                            1
                            • raven-worxR raven-worx

                              @Naveen_D
                              to add up to @sierdzio
                              also remove the line MySender= new Globaltest; from your output() method.
                              Instead call it once before you set it as context property.

                              sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #20

                              @raven-worx said in How to catch the signal emitted from C++ global function in QML:

                              also remove the line MySender= new Globaltest; from your output() method.

                              Doh, I did not even look at that method when writing my reply. Good hint, thanks for being so attentive @raven-worx :-)

                              (Z(:^

                              1 Reply Last reply
                              0
                              • Naveen_DN Offline
                                Naveen_DN Offline
                                Naveen_D
                                wrote on last edited by
                                #21

                                @raven-worx @sierdzio
                                yes when i allocate memory before setcontext property it worked...
                                thanks alot for your help...:-)

                                Naveen_D

                                1 Reply Last reply
                                1
                                • sierdzioS Offline
                                  sierdzioS Offline
                                  sierdzio
                                  Moderators
                                  wrote on last edited by
                                  #22

                                  Cool, good to know :-) Happy coding!

                                  (Z(:^

                                  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