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. Qt 4.8, How to send signal from different thread?
Qt 6.11 is out! See what's new in the release blog

Qt 4.8, How to send signal from different thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 624 Views 2 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.
  • S Offline
    S Offline
    sbahadirarslan
    wrote on last edited by
    #1

    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.

    AndeolA 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S sbahadirarslan

        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.

        AndeolA Offline
        AndeolA Offline
        Andeol
        wrote on last edited by Andeol
        #3

        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

        Developer for R++ : https://rplusplus.com/

        1 Reply Last reply
        0

        • Login

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