Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connection is not processed in thread



  • 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?



  • @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?



  • The same problem with QThread.


  • Lifetime Qt Champion

    @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.


Log in to reply