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. Threads and qml (error on signal)
Forum Updated to NodeBB v4.3 + New Features

Threads and qml (error on signal)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 668 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I'm trying to create a worker thread that will emit a signal to be picked up in my QML. When I emit the signal I get an error:

    QObject: Cannot create children for a parent that is in a different thread.
    

    followed by a segmentation fault.

    I've looked through the wiki, and it seems like I'm doing everything right. Here's the code, as brief as I can make it:

    class Manager : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QByteArray replyData MEMBER m_replyData READ replyData WRITE setReplyData NOTIFY replyDataChanged)
    public:
        explicit Manager() {}
        QByteArray replyData() { return m_replyData; }
    signals:
        void replyDataChanged(QString newReply);
    private:
        QByteArray m_replyData;
    private slots:
        void setReplyData(QByteArray reply) {
            if (reply != m_replyData) {
                m_replyData = reply;
                emit replyDataChanged(m_replyData); // <== this causes the error.
            }
        }
    };
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        Manager manager;
        QThread thread(&app);
        QQmlApplicationEngine engine;
        int rc;
    
        engine.rootContext()->setContextProperty("manager", &manager);
    
        manager.moveToThread(&thread);
        QObject::connect(&thread, &QThread::finished, &manager, &QObject::deleteLater);
        thread.start();
    
        rc = app.exec();
        ...
    }
    

    I'm including the QML related code on the off change that it might have something to do with this problem. Can anyone see what I might be doing wrong here?

    Thanks...

    I jeremy_kJ 2 Replies Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I'm trying to create a worker thread that will emit a signal to be picked up in my QML. When I emit the signal I get an error:

      QObject: Cannot create children for a parent that is in a different thread.
      

      followed by a segmentation fault.

      I've looked through the wiki, and it seems like I'm doing everything right. Here's the code, as brief as I can make it:

      class Manager : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QByteArray replyData MEMBER m_replyData READ replyData WRITE setReplyData NOTIFY replyDataChanged)
      public:
          explicit Manager() {}
          QByteArray replyData() { return m_replyData; }
      signals:
          void replyDataChanged(QString newReply);
      private:
          QByteArray m_replyData;
      private slots:
          void setReplyData(QByteArray reply) {
              if (reply != m_replyData) {
                  m_replyData = reply;
                  emit replyDataChanged(m_replyData); // <== this causes the error.
              }
          }
      };
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          Manager manager;
          QThread thread(&app);
          QQmlApplicationEngine engine;
          int rc;
      
          engine.rootContext()->setContextProperty("manager", &manager);
      
          manager.moveToThread(&thread);
          QObject::connect(&thread, &QThread::finished, &manager, &QObject::deleteLater);
          thread.start();
      
          rc = app.exec();
          ...
      }
      

      I'm including the QML related code on the off change that it might have something to do with this problem. Can anyone see what I might be doing wrong here?

      Thanks...

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #4

      @mzimmers said in Threads and qml (error on signal):

      class Manager : public QObject
      {
      };
      
      int main(int argc, char *argv[])
      {
          engine.rootContext()->setContextProperty("manager", &manager);
      
          manager.moveToThread(&thread);
          ...
      }
      

      I suspect the context property pointing to a QObject with affinity to another thread is the issue.

      As @piervalli suggests, leave the Manager object in the UI thread, and give it a private worker QObject that performs the operations in another thread.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      mzimmersM 1 Reply Last reply
      1
      • mzimmersM mzimmers

        Hi all -

        I'm trying to create a worker thread that will emit a signal to be picked up in my QML. When I emit the signal I get an error:

        QObject: Cannot create children for a parent that is in a different thread.
        

        followed by a segmentation fault.

        I've looked through the wiki, and it seems like I'm doing everything right. Here's the code, as brief as I can make it:

        class Manager : public QObject
        {
            Q_OBJECT
            Q_PROPERTY(QByteArray replyData MEMBER m_replyData READ replyData WRITE setReplyData NOTIFY replyDataChanged)
        public:
            explicit Manager() {}
            QByteArray replyData() { return m_replyData; }
        signals:
            void replyDataChanged(QString newReply);
        private:
            QByteArray m_replyData;
        private slots:
            void setReplyData(QByteArray reply) {
                if (reply != m_replyData) {
                    m_replyData = reply;
                    emit replyDataChanged(m_replyData); // <== this causes the error.
                }
            }
        };
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
            Manager manager;
            QThread thread(&app);
            QQmlApplicationEngine engine;
            int rc;
        
            engine.rootContext()->setContextProperty("manager", &manager);
        
            manager.moveToThread(&thread);
            QObject::connect(&thread, &QThread::finished, &manager, &QObject::deleteLater);
            thread.start();
        
            rc = app.exec();
            ...
        }
        

        I'm including the QML related code on the off change that it might have something to do with this problem. Can anyone see what I might be doing wrong here?

        Thanks...

        I Offline
        I Offline
        IHaber
        wrote on last edited by
        #2

        looking at this code i cant tell the exact issue, but that error you get whenever you call a function from one thread that attempts to create an object whose parent is in another thread.

        An example would be if somewhere in your qml code it called a function of Manager that created an object with parent of Manager

        As for the crash, it's probably because manager is not on the gui thread and QML really doesnt like communicating with anything not on the gui thread.

        piervalliP 1 Reply Last reply
        1
        • I IHaber

          looking at this code i cant tell the exact issue, but that error you get whenever you call a function from one thread that attempts to create an object whose parent is in another thread.

          An example would be if somewhere in your qml code it called a function of Manager that created an object with parent of Manager

          As for the crash, it's probably because manager is not on the gui thread and QML really doesnt like communicating with anything not on the gui thread.

          piervalliP Offline
          piervalliP Offline
          piervalli
          wrote on last edited by piervalli
          #3

          @IHaber
          Only main thread can modify qml object, you should pass the result from thread to main thread.

          1 Reply Last reply
          1
          • mzimmersM mzimmers

            Hi all -

            I'm trying to create a worker thread that will emit a signal to be picked up in my QML. When I emit the signal I get an error:

            QObject: Cannot create children for a parent that is in a different thread.
            

            followed by a segmentation fault.

            I've looked through the wiki, and it seems like I'm doing everything right. Here's the code, as brief as I can make it:

            class Manager : public QObject
            {
                Q_OBJECT
                Q_PROPERTY(QByteArray replyData MEMBER m_replyData READ replyData WRITE setReplyData NOTIFY replyDataChanged)
            public:
                explicit Manager() {}
                QByteArray replyData() { return m_replyData; }
            signals:
                void replyDataChanged(QString newReply);
            private:
                QByteArray m_replyData;
            private slots:
                void setReplyData(QByteArray reply) {
                    if (reply != m_replyData) {
                        m_replyData = reply;
                        emit replyDataChanged(m_replyData); // <== this causes the error.
                    }
                }
            };
            
            int main(int argc, char *argv[])
            {
                QGuiApplication app(argc, argv);
                Manager manager;
                QThread thread(&app);
                QQmlApplicationEngine engine;
                int rc;
            
                engine.rootContext()->setContextProperty("manager", &manager);
            
                manager.moveToThread(&thread);
                QObject::connect(&thread, &QThread::finished, &manager, &QObject::deleteLater);
                thread.start();
            
                rc = app.exec();
                ...
            }
            

            I'm including the QML related code on the off change that it might have something to do with this problem. Can anyone see what I might be doing wrong here?

            Thanks...

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @mzimmers said in Threads and qml (error on signal):

            class Manager : public QObject
            {
            };
            
            int main(int argc, char *argv[])
            {
                engine.rootContext()->setContextProperty("manager", &manager);
            
                manager.moveToThread(&thread);
                ...
            }
            

            I suspect the context property pointing to a QObject with affinity to another thread is the issue.

            As @piervalli suggests, leave the Manager object in the UI thread, and give it a private worker QObject that performs the operations in another thread.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            mzimmersM 1 Reply Last reply
            1
            • jeremy_kJ jeremy_k

              @mzimmers said in Threads and qml (error on signal):

              class Manager : public QObject
              {
              };
              
              int main(int argc, char *argv[])
              {
                  engine.rootContext()->setContextProperty("manager", &manager);
              
                  manager.moveToThread(&thread);
                  ...
              }
              

              I suspect the context property pointing to a QObject with affinity to another thread is the issue.

              As @piervalli suggests, leave the Manager object in the UI thread, and give it a private worker QObject that performs the operations in another thread.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #5

              @jeremy_k yeah, I did just that. Still got the error, but it was due to another object being creating in the worker c'tor, then accessed by the worker. I deferred the object creation, and now it's fixed.

              Thanks to all who looked.

              1 Reply Last reply
              0
              • mzimmersM mzimmers has marked this topic as solved on
              • jeremy_kJ jeremy_k referenced this topic on

              • Login

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