-1

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 ";
        }

    }
}
if4k
  • 19
  • 5
  • seems need to clarify what problem you want to solve – Vladimir Bershov Dec 28 '22 at 07:58
  • *twitches, remembering the debates started by Bradley T. Hughes* You don't need to subclass QThread, that's actually an anti-pattern (sometimes acceptable). Creating a QThread in automatic storage, esp. before creation QApplication is not acceptable. Question is what actually does happen? What is a `className` and how it is connected to the rest of program? "errors" is also too vague. – Swift - Friday Pie Dec 28 '22 at 08:07
  • @VladimirBershov i updated my question. Can you look again? – if4k Dec 28 '22 at 11:08
  • Looks like `moc` is not run on your thread.h - what build system do you use? Dupe of a lot of questions here - search for `staticMetaObject` – chehrlic Dec 28 '22 at 14:08
  • Quite a few issues with you edit... what you're having is ... compilation errors? what is class `mclass` in, I suppose, `mclass.cpp`? Is it what you named `tthread` in your question? – Atmo Dec 29 '22 at 14:00
  • In your C++ code, there is plenty to correct too: you never actually starts the `QThread` instances `t1`, `t2`, `t3`, you call `tthread::start` from the main thread. Worse, you wrote infinite loops in`tthread::secondd`, `tthread::minutee` and `tthread::hourr`. That means signals will be emitted infinitely many times by of the loops, not because of your signal/slot connection (except the first time). You need the methods to return for the event loops to execute. And with no sleep time in the loops, you will not be able to read anything from the `qDebug()` output stream... – Atmo Dec 29 '22 at 14:04

1 Answers1

0

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 not 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.

Bilal Can
  • 39
  • 1
  • 5
  • Dediğin gibi yaptım ve düzeldi. while içerisinde bir kontrol parametresi koydum. Örnek olarak while 20 kez çalışacak şekilde ve while 10.kez çalıştığında emit signal yapacak ve while dan çıkacak şekilde ayarladım. Ama signal emit için while'ın 10 olmasını değil tamamen bitmesini bekliyor. bool parametresi korsam hiç çıkmıyor. Yanlış bir şekilde mi yapıyorum? – if4k Jan 04 '23 at 08:33
  • I can say nothing without seeing your code – Bilal Can Jan 13 '23 at 12:52