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. Connect() returns true but SLOT function is not working
QtWS25 Last Chance

Connect() returns true but SLOT function is not working

Scheduled Pinned Locked Moved Solved General and Desktop
qt guithreadscallbackconnect problem
2 Posts 1 Posters 2.1k Views
  • 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.
  • R Offline
    R Offline
    R_Irudezu
    wrote on 7 Nov 2018, 23:14 last edited by R_Irudezu 11 Jul 2018, 23:26
    #1

    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?

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      R_Irudezu
      wrote on 8 Nov 2018, 00:44 last edited by
      #2

      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

      Keizoku wa chikaranari.

      1 Reply Last reply
      1

      1/2

      7 Nov 2018, 23:14

      • Login

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