QThread signals wont work



  • Hi,

    I calculating model and without about 16 threads Im post for few hours. My problem is that receiving function is not called.

    My code
    @ QThread* thread = new QThread();

                                FaceWorker* worker = new FaceWorker(vertexs, i);
                                worker->moveToThread(thread);
    
                                connect(thread, &QThread::finished, worker, &FaceWorker::deleteLater);
                                connect(this, &Face::operate, worker, &FaceWorker::doWork);
                                connect(worker, &FaceWorker::resultReady, this, &Face::handleResults);
    
                                thread->start();@
    

    handleResults is not called

    @void Face::handleResults(int threadID)
    {
    qDebug() << "thread" << threadID << "has done its work";
    }@

    @void FaceWorker::doWork()
    {
    qDebug() << "emiting results" << threadNumber;

    emit resultReady( threadNumber);
    

    }@
    This is called ok

    @
    class Face : public QObject
    {
    Q_OBJECT

    public:
    Face(QObject* parent = NULL);
    ~Face();

    void optimalize();
    

    public slots:
    void handleResults(int threadID);

    signals:
    void operate();
    };

    class FaceWorker : public QObject
    {
    Q_OBJECT

    public:
    FaceWorker(int threadID, QObject* parent = NULL);
    ~FaceWorker();

    private:

    int threadNumber;
    

    public slots:
    void doWork();

    signals:
    void resultReady(int threadID);
    };@

    Sorry for formatting.. iPad

    Thanks for ideas and help



  • Hi,

    The code looks OK. If the debug message in doWork() is printed, than handleResults() should be called. But to be sure, you can check that the connections are really made:
    @
    if ( ! connect(...) )
    {
    //Houston, we have a problem!
    }
    @

    What is the second parameter 'i' passed to the FaceWorker constructor? If i is the parent, this is not the right thing to do because you "can't":http://doc.qt.io/qt-5/qobject.html#moveToThread move an object with a parent to another thread. I don't know whether this has anything to do with your problem.



  • Connects were ok... I just made it another way, bit hacky but I didnt have time for solving it. Other connections were working like MainWindow, OpenGLWidget, etc.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.