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

Connect() returns true but SLOT function is not working



  • There is two class in project and an external library. External library has a callback function. I am trying to process this callback function in other thread and send its outputs to GUI thread. Remember callback function is not a member. Here what i did (right or wrong):

    mainwindow.h;

    #include "workerthread.h" // thread class that callback function is in
    
    namespace Ui {
    class MainWindow;
    }
    Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        workerThread *callBackThread;
    
    public slots:
        void onLocationChanged(int, int, int, int);
    
    private:
        Ui::MainWindow *ui;
    
    };
    

    mainwindow.cpp;

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "workerthread.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
         threadcallBack = new workerThread(this);
         bool callBackThreadConn = connect(callBackThread, SIGNAL(sendLocationsFromThread(int, int, int, int)), this, SLOT(onLocationChanged(int, int, int, int)));
    
        // next qDebug returns true
        qDebug() << "\n\nRESULT threadcallBack  -  connect returned: " << callBackThreadConn << "\n\n";
        threadcallBack->start();
    }
    MainWindow::~MainWindow()
    {
        delete ui;
        threadcallBack->terminate(); //
    }
    
    //this functions content need to be shown but nothing displays in application output or in GUI
    void MainWindow::onLocationChanged (int x, int y, int w, int h)
    {
        qDebug() << "_______________ faceX: " << x;
        qDebug() << "_______________ faceY: " << y;
        qDebug() << "_______________ faceH: " << h;
        qDebug() << "_______________ faceW: " << w;
        ui->textEdit_2->append(QString::number(x));
        ui->textEdit_2->append(QString::number(y));
        ui->textEdit_2->append(QString::number(w));
        ui->textEdit_2->append(QString::number(h));
    
        qDebug() << "in onLocationChanged function current thread_id : " << QThread::currentThreadId();
        qDebug() << "in onLocationChanged function  current thread: " << QThread::currentThread();
    }
    

    workerthread.h;

    #include <QThread>
    #include <QtCore>
    
    class workerThread : public QThread
    {
        Q_OBJECT
    public:
        explicit workerThread(QObject *parent = 0);
    
        //void run();
        QMutex mutex;
    
    signals:
        void sendLocationsFromThread(int, int, int, int);
    
    public slots:
    };
    

    workerthread.cpp;

    #include "mainwindow.h"
    #include "workerthread.h"
    
    //global workerthread object
    workerThread *t = new workerThread();
    
    void Callback(some_params)
    {
            for (uint32_t i = 0; i < nfaces; i++)
            {
                t->mutex.lock();
    
                emit  t->sendLocationsFromThread(x, y, w, h);
    
                qDebug() << "\033[0;1min Callback function current thread_id : " << QThread::currentThreadId();
                qDebug() << "\033[0;1min Callback function  current thread: " << QThread::currentThread();
    
                t->mutex.unlock();
            }
    }
    
    workerThread::workerThread(QObject *parent) : QThread(parent)
    {
        //constructor
    }
    

    As you see all i want to do is send a couple of int data to GUI thread and show them in a textEdit in the MainWindow. When a function is member of workerThread class, it's ok, data shown in GUI whit emitting. This callBack connect returns true but nothing shown in GUI. Any idea?



  • i did a very bad mistake...

    The mistake is creating two instance of workerThread class, so connection returns true because of the instance of workerThread is just emitting but other instance is just connecting. So it needs to be only one instance and one connection in my case.

    added to workerthread.h:

    extern workerThread *t1;
    

    changed in workerthread.cpp

    emit  t1->sendLocationsFromThread(x, y, w, h);
    

    changed in mainwindow.cpp

    bool callBackThreadConn = connect(w1, SIGNAL(sendLocationsFromThread(int, int, int, int)), this, SLOT(onLocationChanged(int, int, int, int)));
    
    

    it's ok now, data can be send to GUI



  • i did a very bad mistake...

    The mistake is creating two instance of workerThread class, so connection returns true because of the instance of workerThread is just emitting but other instance is just connecting. So it needs to be only one instance and one connection in my case.

    added to workerthread.h:

    extern workerThread *t1;
    

    changed in workerthread.cpp

    emit  t1->sendLocationsFromThread(x, y, w, h);
    

    changed in mainwindow.cpp

    bool callBackThreadConn = connect(w1, SIGNAL(sendLocationsFromThread(int, int, int, int)), this, SLOT(onLocationChanged(int, int, int, int)));
    
    

    it's ok now, data can be send to GUI