qthread to const qthread *
-
i'm trying to learn about how qt deals with threads. here's the example i use from docs. i build using visual studio, and get the error mentioned in the title.
the example actually uses
&workerThreadin the firstconnect()but that gives bunch of unresolved externals#include "QtGuiApplication2.h" #include <QtWidgets/QApplication> #include <QThread> class Worker : public QObject { Q_OBJECT public slots: void doWork(const QString ¶m) { QString result = "Ready!"; emit resultReady(result); } signals: void resultReady(const QString &result); }; class Controller : public QObject { Q_OBJECT QThread workerThread; public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: void operate(const QString &); }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Controller c; return a.exec(); } -
i'm trying to learn about how qt deals with threads. here's the example i use from docs. i build using visual studio, and get the error mentioned in the title.
the example actually uses
&workerThreadin the firstconnect()but that gives bunch of unresolved externals#include "QtGuiApplication2.h" #include <QtWidgets/QApplication> #include <QThread> class Worker : public QObject { Q_OBJECT public slots: void doWork(const QString ¶m) { QString result = "Ready!"; emit resultReady(result); } signals: void resultReady(const QString &result); }; class Controller : public QObject { Q_OBJECT QThread workerThread; public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: void operate(const QString &); }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Controller c; return a.exec(); }@user4592357 said in qthread to const qthread *:
the example actually uses &workerThread in the first connect()
Because the first argument must be a pointer so you have to pass the address of
workerThreadbut that gives bunch of unresolved externals
No, it's not due to that. can you post the error?
-
@user4592357 said in qthread to const qthread *:
the example actually uses &workerThread in the first connect()
Because the first argument must be a pointer so you have to pass the address of
workerThreadbut that gives bunch of unresolved externals
No, it's not due to that. can you post the error?
Error LNK2019 unresolved external symbol "public: void __cdecl Controller::handleResults(class QString const &)" (?handleResults@Controller@@QEAAXAEBVQString@@@Z) referenced in function "public: __cdecl Controller::Controller(void)" (??0Controller@@QEAA@XZ) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK1120 11 unresolved externals QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\x64\Debug\QtGuiApplication2.exe 1 Warning MSB8017 A circular dependency has been detected while executing custom build commands for item "GeneratedFiles\Debug\main.moc". This may cause incremental build to work incorrectly. QtGuiApplication2 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets 171 Error LNK2001 unresolved external symbol "public: static struct QMetaObject const Controller::staticMetaObject" (?staticMetaObject@Controller@@2UQMetaObject@@B) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: static struct QMetaObject const Worker::staticMetaObject" (?staticMetaObject@Worker@@2UQMetaObject@@B) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual int __cdecl Controller::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Controller@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual int __cdecl Worker::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Worker@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Controller::metaObject(void)const " (?metaObject@Controller@@UEBAPEBUQMetaObject@@XZ) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Worker::metaObject(void)const " (?metaObject@Worker@@UEBAPEBUQMetaObject@@XZ) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual void * __cdecl Controller::qt_metacast(char const *)" (?qt_metacast@Controller@@UEAAPEAXPEBD@Z) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2001 unresolved external symbol "public: virtual void * __cdecl Worker::qt_metacast(char const *)" (?qt_metacast@Worker@@UEAAPEAXPEBD@Z) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2019 unresolved external symbol "public: void __cdecl Controller::operate(class QString const &)" (?operate@Controller@@QEAAXAEBVQString@@@Z) referenced in function "public: __cdecl Controller::Controller(void)" (??0Controller@@QEAA@XZ) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1 Error LNK2019 unresolved external symbol "public: void __cdecl Worker::resultReady(class QString const &)" (?resultReady@Worker@@QEAAXAEBVQString@@@Z) referenced in function "public: __cdecl Controller::Controller(void)" (??0Controller@@QEAA@XZ) QtGuiApplication2 C:\Users\...\Documents\Visual Studio 2017\Projects\QtGuiApplication2\QtGuiApplication2\main.obj 1.pro file:
TEMPLATE = app TARGET = QtGuiApplication2 DESTDIR = ../x64/Debug QT += core widgets gui CONFIG += debug DEFINES += WIN64 QT_DLL QT_WIDGETS_LIB INCLUDEPATH += ./GeneratedFiles \ . \ ./GeneratedFiles/Debug DEPENDPATH += . MOC_DIR += ./GeneratedFiles/debug OBJECTS_DIR += debug UI_DIR += ./GeneratedFiles RCC_DIR += ./GeneratedFiles include(QtGuiApplication2.pri) -
void handleResults(const QString &);has no body. you have to at least have a{}- moc does not support 2
Q_OBJECTmarked classes in the same file. move each class in its own header
@VRonin i did but still getting the same errors
-
@VRonin i did but still getting the same errors
@user4592357
Make sure to clean build folder and rebuild all. -
@user4592357
Make sure to clean build folder and rebuild all.