Home > Mobile >  Qt - QThread Signal&Slot No Error Just not Working
Qt - QThread Signal&Slot No Error Just not Working

Time:01-04

I updated my question. My project is so simple. I want to create 3 thread. Each thread have different operations. Finally, they should communicate using signal&slot.

I got errors:

-debug/main.o:main.cpp:(.rdata$.refptr._ZTV6mclass[.refptr._ZTV6mclass] 0x0): undefined reference to `vtable for mclass'

-debug/main.o:main.cpp:(.rdata$.refptr._ZN6mclass16staticMetaObjectE[.refptr._ZN6mclass16staticMetaObjectE] 0x0): undefined reference to `mclass::staticMetaObject'

-debug/main.o:main.cpp:(.rdata$.refptr._ZN6mclass7sender2Ev[.refptr._ZN6mclass7sender2Ev] 0x0): undefined reference to `mclass::sender2()'

-debug/main.o:main.cpp:(.rdata$.refptr._ZN6mclass7sender1Ei[.refptr._ZN6mclass7sender1Ei] 0x0): undefined reference to `mclass::sender1(int)'

-debug/mclass.o: in function `mclass::start()':

-\qtProjects\triple\mclass.cpp:20: error: undefined reference to `mclass::sender1(int)

-\qtProjects\build-triple-Desktop_Qt_6_3_2_MinGW_64_bit-Debug/../triple/mclass.cpp:22: undefined reference to `mclass::sender2()'

-collect2.exe: error: ld returned 1 exit status

-[Makefile.Debug:71: debug/triple.exe] Error 1

I change : public QObject to QThread and I got same errors again.

Thanks for helping.

tthread.h file

#ifndef TTHREAD_H
#define TTHREAD_H
#include <QThread>
#include <QObject>
#include <QDebug>

class tthread : public QObject
{
        Q_OBJECT
public:
    explicit tthread(QObject *parent = nullptr);
    void secondd();
    void start();

signals:
    void sec(int value);
    void minu(int value);

public slots:
    void minutee(int value);
    void hourr(int value);
};

#endif // TTHREAD_H

main.cpp file

#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <QObject>
#include "tthread.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QThread t1;
    QThread t2;
    QThread t3;

    tthread thread1;
    tthread thread2;
    tthread thread3;

    thread1.moveToThread(&t1);
    thread2.moveToThread(&t2);
    thread3.moveToThread(&t3);

    QObject::connect(&thread1, &tthread::sec, &thread2, &tthread::minutee);
    QObject::connect(&thread2, &tthread::minu, &thread3, &tthread::hourr);

    thread1.start();
    thread2.start();
    thread3.start();

    return a.exec();
}

tthread.cpp file

#include "tthread.h"

tthread::tthread(QObject *parent) : QObject(parent)
{

}

void tthread::start()
{
    qDebug() << "Start" << QThread::currentThread();

}

void tthread::secondd()
{
    int counter = 0;
    int sign = 1;
    while(1)
    {
        counter  ;

        qDebug() << "Secondd Counter: " << counter  << QThread::currentThread();


        if(counter == 5)
        {
            emit sec(sign);
            counter = 0;
            qDebug() << "Signal from sec to min " << QThread::currentThread();
        }
    }
}

void tthread::minutee(int value)
{
    int counter2 = 0;
    int sign2 = 1;
    while(1)
    {
        qDebug() << "Secondd Value: " << value ;
        counter2  ;

        qDebug() << "Secondd Counter: " << counter2 ;


        if(counter2 == 5)
        {
            emit minu(sign2);
            counter2 = 0;
            qDebug() << "Signal from sec to min ";
        }
    }
}


void tthread::hourr(int value)
{
    int counter3 = 0;
    while(1)
    {
        qDebug() << "hourr Value: " << value ;
        counter3  ;

        qDebug() << "hourr Counter: " << counter3 ;


        if(counter3 == 5)
        {

            counter3 = 0;
            qDebug() << "Signal from min to hourr ";
        }

    }
}

CodePudding user response:

convert class tthread : public QObject to class tthread : public QThread at first. Then remove the code with QThread t1, QThread t2, QThread t3. You do no need them since tthread class is a QThread. Start threads with the code below.

->start(QThread::Priority::HighestPriority);

Then override the Run method of QThread

protected:
    void run() override;

Move the content inside secondd method to the Run method, and delete secondd method. Probably you do not start the secondd method. That's why nothing works. Run method is automatically called when you call the start method of a QThread.

By the way, you have infinite loops. You need to break the loop when you want to close the application. Do not use while(1). Instead, use while (boolean_param). In the close event of the application, call some signals&slots to set the boolean_param to false to finish the job in the thread.

  • Related