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. qmlRegisterSingletonType object emit signal. Problem with qml slot connection

qmlRegisterSingletonType object emit signal. Problem with qml slot connection

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml
10 Posts 5 Posters 1.1k 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.
  • E Offline
    E Offline
    edoardo.videx
    wrote on last edited by edoardo.videx
    #1

    Hi, i've a qmlRegisterSingletonType object that's can emit two signals. I want capture this signal with slots in QML.

    This is the main:

    qmlRegisterSingletonType<SDK2220_SysLog>("VidexMessageBox",0,1,"SysLogSocket",Get_SysLogManager);
    

    This is my class :

    class SDK2220_SysLog :public QThread
    {
        Q_OBJECT
    
    
        Q_PROPERTY(NOTIFY set_logger_okay CONSTANT)
    
        Q_PROPERTY(NOTIFY add_new_item CONSTANT)
    
    public:
    
        SDK2220_SysLog();
    
        SDK2220_SysLog(QHostAddress *addr,int port);
    
    
    private slots:
    
        void on_start_thread_emitted();
    
    signals:
    
        Q_INVOKABLE void set_logger_okay();
    
        Q_INVOKABLE void add_new_item();
    
    
    };
    

    and this is a QML CODE:

        Connections{
            target: SysLogSocket
    
            onSet_logger_okay:{
                setting.saveFilterChecked("SRV2220",2)
                console.log("33333333333333333333333333333333333333333333333333333")
            }
        }
    

    When in the cpp code i emit set_logger_okay, it doesen't captured to QML.

    Someone can help me?

    J.HilkJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      Check if there is any warning printed at runtime - perhaps Connections did not really connect?

      Also, try removing the Q_PROPERTY notifiers - just having signals is enough. And you don't need to marks signals as Q_INVOKABLE, they already are invokable.

      (Z(:^

      1 Reply Last reply
      0
      • E edoardo.videx

        Hi, i've a qmlRegisterSingletonType object that's can emit two signals. I want capture this signal with slots in QML.

        This is the main:

        qmlRegisterSingletonType<SDK2220_SysLog>("VidexMessageBox",0,1,"SysLogSocket",Get_SysLogManager);
        

        This is my class :

        class SDK2220_SysLog :public QThread
        {
            Q_OBJECT
        
        
            Q_PROPERTY(NOTIFY set_logger_okay CONSTANT)
        
            Q_PROPERTY(NOTIFY add_new_item CONSTANT)
        
        public:
        
            SDK2220_SysLog();
        
            SDK2220_SysLog(QHostAddress *addr,int port);
        
        
        private slots:
        
            void on_start_thread_emitted();
        
        signals:
        
            Q_INVOKABLE void set_logger_okay();
        
            Q_INVOKABLE void add_new_item();
        
        
        };
        

        and this is a QML CODE:

            Connections{
                target: SysLogSocket
        
                onSet_logger_okay:{
                    setting.saveFilterChecked("SRV2220",2)
                    console.log("33333333333333333333333333333333333333333333333333333")
                }
            }
        

        When in the cpp code i emit set_logger_okay, it doesen't captured to QML.

        Someone can help me?

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @edoardo-videx said in qmlRegisterSingletonType object emit signal. Problem with qml slot connection:

        Get_SysLogManager

        please show us your singleton instance function


        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.

        E 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @edoardo-videx said in qmlRegisterSingletonType object emit signal. Problem with qml slot connection:

          Get_SysLogManager

          please show us your singleton instance function

          E Offline
          E Offline
          edoardo.videx
          wrote on last edited by
          #4

          @J-Hilk

          static QObject * Get_SysLogManager(QQmlEngine * engine, QJSEngine *scriptEngine)
          {
          
          
              Q_UNUSED(engine);
              Q_UNUSED(scriptEngine);
          
              static SDK2220_SysLog * s_instance = new SDK2220_SysLog();
          
              QQmlEngine::setObjectOwnership(s_instance, QQmlEngine::CppOwnership);
          
              return s_instance;
          }
          
          J.HilkJ 1 Reply Last reply
          0
          • E edoardo.videx

            @J-Hilk

            static QObject * Get_SysLogManager(QQmlEngine * engine, QJSEngine *scriptEngine)
            {
            
            
                Q_UNUSED(engine);
                Q_UNUSED(scriptEngine);
            
                static SDK2220_SysLog * s_instance = new SDK2220_SysLog();
            
                QQmlEngine::setObjectOwnership(s_instance, QQmlEngine::CppOwnership);
            
                return s_instance;
            }
            
            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @edoardo-videx alight
            and how and when do you emit the signal from c++ ?


            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.

            E 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @edoardo-videx alight
              and how and when do you emit the signal from c++ ?

              E Offline
              E Offline
              edoardo.videx
              wrote on last edited by
              #6

              @J-Hilk

              Inside the method

              void SDK2220_SysLog::run()
              

              i do this :

              if( evt->cmd == SDK2220_PROTOCOL_CMD_SET_LOGGER)
              {
                    emit set_logger_okay();
              }
              
              KroMignonK 1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Are you 100% sure the signal is actually being emitted? Throw in some debug message to verify.

                (Z(:^

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

                  Also, does that run() method leave some space for even loop to propagate the signal?

                  (Z(:^

                  1 Reply Last reply
                  0
                  • E edoardo.videx

                    @J-Hilk

                    Inside the method

                    void SDK2220_SysLog::run()
                    

                    i do this :

                    if( evt->cmd == SDK2220_PROTOCOL_CMD_SET_LOGGER)
                    {
                          emit set_logger_okay();
                    }
                    
                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by KroMignon
                    #9

                    @edoardo-videx I see SDK220_SysLog got a slot/method called run(). Is it sub-classing QObject or QThread?
                    To made signal/slots works, a QObject class must be running in a thread which implements a event loop... If you have sub-classed QThread and then also the QThread::run() method, then there will be no event queue to handle the signal!

                    You can try to create a temporary QEventLoop to handle your signal:

                    if( evt->cmd == SDK2220_PROTOCOL_CMD_SET_LOGGER)
                    {
                        QEventLoop loop;
                        Q_UNUSED(loop)
                        emit set_logger_okay();
                    }
                    

                    In general, it is not a good practice to sub-class QThread, it is bette to create a worker class and to move this class to new thread.
                    Take a look at following links for more details:
                    http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/
                    https://www.toptal.com/qt/qt-multithreading-c-plus-plus

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    jsulmJ 1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @edoardo-videx I see SDK220_SysLog got a slot/method called run(). Is it sub-classing QObject or QThread?
                      To made signal/slots works, a QObject class must be running in a thread which implements a event loop... If you have sub-classed QThread and then also the QThread::run() method, then there will be no event queue to handle the signal!

                      You can try to create a temporary QEventLoop to handle your signal:

                      if( evt->cmd == SDK2220_PROTOCOL_CMD_SET_LOGGER)
                      {
                          QEventLoop loop;
                          Q_UNUSED(loop)
                          emit set_logger_okay();
                      }
                      

                      In general, it is not a good practice to sub-class QThread, it is bette to create a worker class and to move this class to new thread.
                      Take a look at following links for more details:
                      http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/
                      https://www.toptal.com/qt/qt-multithreading-c-plus-plus

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @KroMignon said in qmlRegisterSingletonType object emit signal. Problem with qml slot connection:

                      QEventLoop loop;
                      Q_UNUSED(loop)

                      I don't even know what this loop is supposed to do as exec() isn't called here

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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