Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qthread to const qthread *
Forum Updated to NodeBB v4.3 + New Features

qthread to const qthread *

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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 &workerThread in the first connect() 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 &param) {
    		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();
    }
    
    
    VRoninV 1 Reply Last reply
    0
    • U user4592357

      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 &workerThread in the first connect() 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 &param) {
      		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();
      }
      
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @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 workerThread

      but that gives bunch of unresolved externals

      No, it's not due to that. can you post the error?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      U 1 Reply Last reply
      1
      • VRoninV VRonin

        @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 workerThread

        but that gives bunch of unresolved externals

        No, it's not due to that. can you post the error?

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        @VRonin

        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)
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4
          • void handleResults(const QString &); has no body. you have to at least have a {}
          • moc does not support 2 Q_OBJECT marked classes in the same file. move each class in its own header

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          U 1 Reply Last reply
          5
          • VRoninV VRonin
            • void handleResults(const QString &); has no body. you have to at least have a {}
            • moc does not support 2 Q_OBJECT marked classes in the same file. move each class in its own header
            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @VRonin i did but still getting the same errors

            mrjjM 1 Reply Last reply
            0
            • U user4592357

              @VRonin i did but still getting the same errors

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @user4592357
              Make sure to clean build folder and rebuild all.

              VRoninV 1 Reply Last reply
              3
              • mrjjM mrjj

                @user4592357
                Make sure to clean build folder and rebuild all.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                And re-run qmake

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved