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. 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
-
@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 qmakeIf that doesn't help, post your class definition for
myobject
. -
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() { };
};
-
@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. -
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.