I recently successfully implemented multithreading to my GUI application using method by reimplementing run() virtual void, but I read there is a lot of confusion around threading in QT as even documentation doesn't present it correctly. What I'm trying now is just to understand how it works and use it without any issues even not visible ones at first look. I'm wondering what can be wrong in my code below, as I'm getting info from Visual Studio when starting that some errors occured and I see "QtRunWork" task returned false but did not log an error." When I ignore this and start the program I'm getting in console this: "qt.core.qobject.connect: QObject::connect: No such slot QObject::process() in (..)main.cpp:38 qt.core.qobject.connect: QObject::connect: No such signal QObject::finished() in (..)main.cpp:39 qt.core.qobject.connect: QObject::connect: No such signal QObject::finished() in (..)main.cpp:40" "
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QThread>
#include <QString>
#include <QDebug>
class Worker : public QObject {
Q_OBJECT
public slots:
void process() {
qDebug() << "Thread1";
emit finished();
}
signals:
void finished();
};
class MyThread : public QThread {
public:
MyThread();
signals:
void finished();
};
MyThread::MyThread() {
Worker* w = new Worker();
w->moveToThread(this);
connect(this, SIGNAL(started()), w, SLOT(process()));
connect(w, SIGNAL(finished()), this, SLOT(quit()));
connect(w, SIGNAL(finished()), w, SLOT(deleteLater()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
this->start();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread m;
return a.exec();
}