PDA

View Full Version : Error message in QThread



marc2050
17th June 2011, 03:28
Hi

I've a separate thread running.
In that thread, I like to let the user know there is some error.
However, the usual Qmessagebox is not working while running
a thread. Is there any other way to prompt a pop up or
some messages while within a thread?

Thanks for any help. I'm stuck on this for sometime.

Santosh Reddy
17th June 2011, 03:40
Move the QMessageBox to the Main thread, and send a signal to the QMessageBox from the separate tread.

This is how it is supposed to be used, all GUI components should be in main thread only.

marc2050
17th June 2011, 04:05
Sorry for my ignorant. But how do you send a signal from a separate thread?
I can understand we should only use the Qmessagebox in the main thread. But
cant figure out how do I send a signal from the thread?

Santosh Reddy
17th June 2011, 04:27
Refer this example to send signals to Main (GUI) thread from QThread

http://www.qtcentre.org/wiki/index.php?title=Updating_GUI_from_QThread

marc2050
19th June 2011, 08:39
Thanks.
I followed the example but gotten this error during linking.
LNK2019:unresolved external symbol"protected: void __thiscall mythread:valuceChanged(int)"

What I dont get is, this valueChanged is the standard function for signals.
So, do I have to define it again in my own function?
And the example didnt show that we need to define this valueChanged function either.

Any help?

Rachol
19th June 2011, 09:00
Please take a look again at the example in the link that Santosh has provided. There is "Source Files" section, where you have this simple mechanism of connection signals to slots implemented. If you look more closely you will see that valueChanged is a custom signal of MyThread class. Now you have to define your own signal in your own thread.

marc2050
19th June 2011, 12:47
Thanks.
Sorry, but I've been looking at the files and also downloaded them and view them again and again.
There is no indication of how do you and where did it define the valueChanged.
I read that there is the
1. signals:
void valueChanged(int);
2. emit valueChanged(i);

But there is nothing on how do you define this valueChanged function. Do we need the metaobject to define it?
If I compile this, I'll get the error - LNK2019:unresolved external symbol"protected: void __thiscall mythread:valuceChanged(int)".
I'm still confused...

stampede
19th June 2011, 13:11
valueChanged is a signal, and you don't provide implementation of signals, moc does.
Be sure to do a clean build (make clean, qmake, make). Check if moc code is generated for your class (dont forget Q_OBJECT macro).

marc2050
20th June 2011, 00:33
Hi.

I did a clean and rebuild. I also make sure I've Q_Object macro in mythread.
Now I got the following errors. Before I added Q_Object, I've only the LNK2019 error.
Also, can you confirm that even for custom signals like the earlier author Rachol replied,
that I still do not need to provide implementation of the valueChanged function?
If so, how do I resolve these metaobject error below?

Please help. I'm losing my hair over this and cant move on.




mythread.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall mythread::metaObject(void)const " (?metaObject@mythread@@UBEPBUQMetaObject@@XZ)
mythread.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall mythread::qt_metacast(char const *)" (?qt_metacast@mythread@@UAEPAXPBD@Z)
mythread.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall mythread::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@mythread@@UAEHW4Call@QMetaObject@@HP APAX@Z)
mythread.obj:-1: error: LNK2019: unresolved external symbol "protected: void __thiscall mythread::valueChanged(int)" (?valueChanged@mythread@@IAEXH@Z) referenced in function "public: void __thiscall mythread::init(void)" (?init@mythread@@QAEXXZ)
mythread.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const mythread::staticMetaObject" (?staticMetaObject@mythread@@2UQMetaObject@@B)

stampede
20th June 2011, 00:46
You do not provide implementation of any signals, because moc will do that. You need to implement slots if you have any.
This looks like moc code is not compiled, how do you build your project ? If you have a .pro file, post it here. Show the header too, maybe you have some obvious errors, typos etc.

marc2050
20th June 2011, 01:04
Hi.
Below is my .pro file.
I'm using QT creator as my IDE.
Yes. I've implemented my slots.
I think you're right. I'm missing the moc codes.
So, how do I ask QT creator to build moc codes?



#-------------------------------------------------
#
# Project created by QtCreator 2011-04-25T10:51:02
#
#-------------------------------------------------

QT += core gui

TARGET = mylittleProgram
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp \
mylittleheader.cpp \
mylittlerender.cpp \
mylittlereaddata.cpp \
mythread.cpp

HEADERS += mainwindow.h \
mylittleheader.h \
mylittlerender.h \
mylittlereaddata.h \
mythread.h


FORMS += mainwindow.ui

stampede
20th June 2011, 02:06
It should do it automatically.
.pro file looks ok, so I'd start by running qmake and check if Makefiles contains correct moc build targets.