Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Multithreading in Qt with C++
Forum Update on Monday, May 27th 2025

Multithreading in Qt with C++

Scheduled Pinned Locked Moved Solved Game Development
5 Posts 2 Posters 1.3k Views
  • 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.
  • S Offline
    S Offline
    stevereine
    wrote on 1 Aug 2020, 14:58 last edited by
    #1

    I'm trying to create a separate thread to periodically poll another device via TCP/IP. Right now just trying to create and start a separate thread that prints "hello world" to a textbox in my main GUI. I've read dozens of links on how to do this, and tried to rearrange my code in a number of different ways, still no success.

    Here's my thread class;

    class WorkerThread : public QThread
    {
    Q_OBJECT
    public:

    explicit WorkerThread();
    //explicit WorkerThread(QObject *parent);
    ~WorkerThread();
    
    void run() override
    {
      //if (pw!=0)
      //pw->setText1("starting new thread");
    }
    

    signals:
    void settingtext()
    {};
    //void resultReady(const QString &s)
    void resultReady()
    {};
    };

    and here is the class derived from QObject that I use to set this up;

    class WorkerThread : public QThread
    {
    Q_OBJECT
    public:

    explicit WorkerThread();
    //explicit WorkerThread(QObject *parent);
    ~WorkerThread();
    
    void run() override
    {
      //if (pw!=0)
      //pw->setText1("starting new thread");
    }
    

    signals:
    void settingtext()
    {};
    //void resultReady(const QString &s)
    void resultReady()
    {};
    };

    Included with the rest of my code, this compiles and runs fine. However, when I try to create an instance of myobject in my main code, it gives me three linker errors;

    myobject *newobject = new myobject();

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl myobject::metaObject(void)const " (?metaObject@myobject@@UEBAPEBUQMetaObject@@XZ)

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl myobject::qt_metacast(char const *)" (?qt_metacast@myobject@@UEAAPEAXPEBD@Z)

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl myobject::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myobject@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    Does anybody have any hints on this? thanks.....Steve

    J 1 Reply Last reply 2 Aug 2020, 07:40
    0
    • S stevereine
      1 Aug 2020, 14:58

      I'm trying to create a separate thread to periodically poll another device via TCP/IP. Right now just trying to create and start a separate thread that prints "hello world" to a textbox in my main GUI. I've read dozens of links on how to do this, and tried to rearrange my code in a number of different ways, still no success.

      Here's my thread class;

      class WorkerThread : public QThread
      {
      Q_OBJECT
      public:

      explicit WorkerThread();
      //explicit WorkerThread(QObject *parent);
      ~WorkerThread();
      
      void run() override
      {
        //if (pw!=0)
        //pw->setText1("starting new thread");
      }
      

      signals:
      void settingtext()
      {};
      //void resultReady(const QString &s)
      void resultReady()
      {};
      };

      and here is the class derived from QObject that I use to set this up;

      class WorkerThread : public QThread
      {
      Q_OBJECT
      public:

      explicit WorkerThread();
      //explicit WorkerThread(QObject *parent);
      ~WorkerThread();
      
      void run() override
      {
        //if (pw!=0)
        //pw->setText1("starting new thread");
      }
      

      signals:
      void settingtext()
      {};
      //void resultReady(const QString &s)
      void resultReady()
      {};
      };

      Included with the rest of my code, this compiles and runs fine. However, when I try to create an instance of myobject in my main code, it gives me three linker errors;

      myobject *newobject = new myobject();

      main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl myobject::metaObject(void)const " (?metaObject@myobject@@UEBAPEBUQMetaObject@@XZ)

      main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl myobject::qt_metacast(char const *)" (?qt_metacast@myobject@@UEAAPEAXPEBD@Z)

      main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl myobject::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myobject@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

      Does anybody have any hints on this? thanks.....Steve

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 2 Aug 2020, 07:40 last edited by
      #2

      @stevereine said in Multithreading in Qt with C++:

      I'm trying to create a separate thread to periodically poll another device via TCP/IP. Right now just trying to create and start a separate thread that prints "hello world" to a textbox in my main GUI.

      You don't need a thread to periodically poll a device via TCP/IP. Just use a QTimer.

      Also, you are not allowed to touch any GUI classes from a secondary thread. GUI class methods can only be called from the GUI thread (which is the thread that creates the QApplication/QGuiApplication object).

      @stevereine said in Multithreading in Qt with C++:

      it gives me three linker errors;
      ...
      main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl myobject::metaObject(void)const " (?metaObject@myobject@@UEBAPEBUQMetaObject@@XZ)

      Try running qmake. If you are using Qt Creator, the command is in the top menu bar: Build > Run qmake

      If that doesn't help, post your class definition for myobject.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • S Offline
        S Offline
        stevereine
        wrote on 2 Aug 2020, 13:58 last edited by
        #3

        My apologies, I cut and pasted my QThread class twice, where I meant to post the code for my myobject class. Here's the myobject class.

        I did run qmake, the result is the same linker errors

        class myobject : public QObject
        {
        Q_OBJECT
        QThread workerThread;

        public:
        void startWorkInAThread()
        {
        //WorkerThread workerThread;
        //connect(workerThread, &WorkerThread::resultReady, this, &myobject::handleResults);
        //connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
        workerThread.start();
        }

        void handleResults()
        {
        
        };
        

        };

        J 1 Reply Last reply 2 Aug 2020, 14:54
        0
        • S stevereine
          2 Aug 2020, 13:58

          My apologies, I cut and pasted my QThread class twice, where I meant to post the code for my myobject class. Here's the myobject class.

          I did run qmake, the result is the same linker errors

          class myobject : public QObject
          {
          Q_OBJECT
          QThread workerThread;

          public:
          void startWorkInAThread()
          {
          //WorkerThread workerThread;
          //connect(workerThread, &WorkerThread::resultReady, this, &myobject::handleResults);
          //connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
          workerThread.start();
          }

          void handleResults()
          {
          
          };
          

          };

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 2 Aug 2020, 14:54 last edited by
          #4

          @stevereine said in Multithreading in Qt with C++:

          I did run qmake, the result is the same linker errors

          I forgot to mention: Make sure your class definitions are in *.h files, not in *.cpp files.

          Again, I highly recommend you try to achieve your goal using a QTimer instead of threads.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3
          • S Offline
            S Offline
            stevereine
            wrote on 2 Aug 2020, 22:43 last edited by
            #5

            Hello JKSH,

            It's working now, thanks to your suggestions.

            I broke the QThread and QObject classes out into .h and .cpp files, pretty much worked after that. Also, I hit a dead end trying to periodically start the thread to poll for TCP/IP data. That's an easy thing to do in Python, not so elegant in QT C++. I went with your recommendation on using QTimer. All seems well with the app now. Thanks again, I am submitting this as solved.

            1 Reply Last reply
            2

            1/5

            1 Aug 2020, 14:58

            • Login

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