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

Qt 4.8, How to send signal from different thread?



  • I am trying to send signal across threads.
    For testing this situation, I wrote a test code.
    I am working on ubuntu 16.04 machine and qt version is 4.8.

    In my code, three class exists ;
    1 - timer_class -> in this class, I emit signal in timeout slot.
    2 - test_worker -> I am using this class as thread's worker.
    3 - main_class -> I create timer_class instance, and also create thread in this class constructor.
    
    I am trying to connect timer_class signal to test_worker slot.
    Here is my code;
    
    First; timer_class :
    

    Header File:

    #include "timer_class.h"
    
    timer_class::timer_class(QObject *parent)
        :QObject(parent)
    {
        timer = new QTimer();
        connect(timer, SIGNAL(timeout()), this, SLOT(on_timeout_occur()));
    }
    
    timer_class::~timer_class()
    {
    
    }
    
    void timer_class::start_timer()
    {
        timer->start(1000);
    }
    
    void timer_class::on_timeout_occur()
    {
        qDebug() << "timeout occur";
        emit dummy_signal();
    }
    

    Source File :

    #include "timer_class.h"
    
    timer_class::timer_class(QObject *parent)
        :QObject(parent)
    {
        timer = new QTimer();
        connect(timer, SIGNAL(timeout()), this, SLOT(on_timeout_occur()));
    }
    
    timer_class::~timer_class()
    {
    
    }
    
    void timer_class::start_timer()
    {
        timer->start(1000);
    }
    
    void timer_class::on_timeout_occur()
    {
        qDebug() << "timeout occur";
        emit dummy_signal();
    }
    

    Second, thread_worker class :
    Header File :

    #ifndef THREAD_WORKER_H
    #define THREAD_WORKER_H
    
    #include <QDebug>
    #include <QObject>
    
    class thread_worker : public QObject
    {
        Q_OBJECT
    public:
        thread_worker(QObject *parent = 0);
        ~thread_worker();
    
    public slots:
        void main_loop();
        void on_dummy_signal();
    };
    
    #endif // THREAD_WORKER_H
    

    Source File :

    #include "thread_worker.h"
    
    thread_worker::thread_worker(QObject *parent)
        :QObject(parent)
    {
    
    }
    
    thread_worker::~thread_worker()
    {
    
    }
    
    void thread_worker::main_loop()
    {
        forever
        {
            //qDebug() << "In Main Loop";
        }
    }
    
    void thread_worker::on_dummy_signal()
    {
        qDebug() << "dummy signal received";
    }
    

    And last, main_class :
    Header file :

    #ifndef MAIN_CLASS_H
    #define MAIN_CLASS_H
    
    #include <QObject>
    #include <QDebug>
    #include <QThread>
    #include "timer_class.h"
    #include "thread_worker.h"
    
    class main_class : public QObject
    {
        Q_OBJECT
    public:
        main_class(QObject *parent = 0);
        ~main_class();
    
    private:
        QThread *thread;
        timer_class *tmr_class;
        thread_worker *worker;
    };
    
    #endif // MAIN_CLASS_H
    

    Source File:

    #include "main_class.h"
    
    main_class::main_class(QObject *parent)
        :QObject(parent)
    {
        tmr_class   = new timer_class();
        worker      = new thread_worker();
        thread      = new QThread();
    
        worker->moveToThread(thread);
        connect(tmr_class, SIGNAL(dummy_signal()), worker, SLOT(on_dummy_signal()));
    
        connect(thread, SIGNAL(started()), worker, SLOT(main_loop()));
        thread->start();
        tmr_class->start_timer();
    }
    
    main_class::~main_class()
    {
    
    }
    

    In main.cpp, I just create main_class instance like this :

    #include <QCoreApplication>
    #include "main_class.h"
    
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            main_class *main_cl = new main_class();
        
            qDebug() << "executing a.exec";
            return a.exec();
        }
    

    When I run the application, I saw "timeout occur" in console, but I don't saw "dummy signal received". I can not find problem.

    Could you help me about this problem ?
    Thanks.


  • Lifetime Qt Champion

    Hi,

    It's a pretty convoluted setup to use a timer. What's your exact goal ?

    Also that your timer object won't be moved to the worker thread as you didn't give him a parent.

    Out of curiosity, why are you using Qt 4.8 ?



  • I think your method should work. But I don't see the declaration for your dummy_signal.
    In timer_class header file, you should have

    signals: 
    void dummy_signal();
    

    I'm surprised your compiler doesn't complain if that's not the case.
    Also, you define on_timeout_occur both in the header and the cpp? I guess that's just a copy-paste issue. You put your .cpp content also in the .h


Log in to reply