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. Connection is not processed in thread
Forum Updated to NodeBB v4.3 + New Features

Connection is not processed in thread

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

    Hi,
    For some reason connection is not processed in thread.
    Thread contains an infinite loop with QCoreApplication::processEvents();

    Minimal code reproduction.
    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <thread>
    #include "objectinthread.h"
    
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
    
        auto t = std::thread([]()
        {
            ObjectInThread objectInThread;
            while(1)
            {
                QCoreApplication::processEvents();
                std::this_thread::yield();
            }
        });
        t.detach();
    
        return app.exec();
    }
    

    objectinthread.H:

    #pragma once
    #include <QCoreApplication>
    #include <QObject>
    #include <QDebug>
    #include <thread>
    #include <chrono>
    
    class ObjectInThread2 :
        public QObject
    {
        Q_OBJECT
    public:
    
    public slots:
        void test()
        {
            qDebug() << "success";
        }
    };
    
    class ObjectInThread :
        public QObject
    {
        Q_OBJECT
    public:
        ObjectInThread()
        {
            auto t = std::thread([this]()
            {
                ObjectInThread2 objectInThread2;
                connect
                (
                    this,
                    &ObjectInThread::emitSignal,
                    &objectInThread2,
                    &ObjectInThread2::test
                );
                while (1)
                {
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                    QCoreApplication::processEvents();
                    qDebug() << "processEvents";
                }
            });
            t.detach();
            while (1)
            {
                std::this_thread::sleep_for(std::chrono::seconds(5));
                qDebug() << "emit";
                emit emitSignal();
            }
        }
    
    signals:
        void emitSignal();
    
    };
    

    Output:

    processEvents
    processEvents
    processEvents
    processEvents
    processEvents
    emit
    processEvents
    processEvents
    processEvents
    

    Slot test() is never used.
    Any idea how to fix it?

    JonBJ 1 Reply Last reply
    0
    • D Devoo

      Hi,
      For some reason connection is not processed in thread.
      Thread contains an infinite loop with QCoreApplication::processEvents();

      Minimal code reproduction.
      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <thread>
      #include "objectinthread.h"
      
      
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
      
          auto t = std::thread([]()
          {
              ObjectInThread objectInThread;
              while(1)
              {
                  QCoreApplication::processEvents();
                  std::this_thread::yield();
              }
          });
          t.detach();
      
          return app.exec();
      }
      

      objectinthread.H:

      #pragma once
      #include <QCoreApplication>
      #include <QObject>
      #include <QDebug>
      #include <thread>
      #include <chrono>
      
      class ObjectInThread2 :
          public QObject
      {
          Q_OBJECT
      public:
      
      public slots:
          void test()
          {
              qDebug() << "success";
          }
      };
      
      class ObjectInThread :
          public QObject
      {
          Q_OBJECT
      public:
          ObjectInThread()
          {
              auto t = std::thread([this]()
              {
                  ObjectInThread2 objectInThread2;
                  connect
                  (
                      this,
                      &ObjectInThread::emitSignal,
                      &objectInThread2,
                      &ObjectInThread2::test
                  );
                  while (1)
                  {
                      std::this_thread::sleep_for(std::chrono::seconds(1));
                      QCoreApplication::processEvents();
                      qDebug() << "processEvents";
                  }
              });
              t.detach();
              while (1)
              {
                  std::this_thread::sleep_for(std::chrono::seconds(5));
                  qDebug() << "emit";
                  emit emitSignal();
              }
          }
      
      signals:
          void emitSignal();
      
      };
      

      Output:

      processEvents
      processEvents
      processEvents
      processEvents
      processEvents
      emit
      processEvents
      processEvents
      processEvents
      

      Slot test() is never used.
      Any idea how to fix it?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Devoo
      You may well know more than I do about this, but if this is to work at all across threads should you be using QThread and its support rather than std::thread?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Devoo
        wrote on last edited by Devoo
        #3

        The same problem with QThread.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Devoo said in Connection is not processed in thread:

          With Qthread the same issue.

          Please take a look at the QThread documentation. Esp. about the thread-local eventloop. You're not calling / executing it so it will not work.

          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