Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to update a label from another thread (correct method of signal/slots & threading)?[SOLVED]
below is code for a basic thread that counts to 100. How would i implement this console application in a gui application (specifically having the thread started and stoped by a push button and displaying the current number in a label), any help would be a massive help as struggling to get my head round this problem, i presume it's related to signal and slots however not sure how I would link the count method to a label. Anyone have any suggestions code listed below:
int main(int argc, char *argv)
QCoreApplication a(argc, argv);
QThread thread; Object thread_object; thread_object.setup(thread); thread_object.moveToThread(&thread); thread.start(); return a.exec();
Object::Object(QObject *parent) :
void Object::setup(QThread &thread)
for(int i = 0; i < 100; i++)
qDebug() << i;
class Object : public QObject
explicit Object(QObject *parent = 0);
void setup(QThread &thread);
#endif // OBJECT_H
Please don't post the "almost same question":http://qt-project.org/forums/viewthread/31533/ multiple times.
There have been several discussions on this forum about how to communicate between threads and the GUI thread recently. A little bit of searching should bring you what need
Well firstly, I have been looking on here for a while for a basic example. Of the ones that do talk about threading don't include full code for example "TCP/ip Thread example":http://qt-project.org/forums/viewthread/31382/ doesn't include any code. Also "Qt Threading - How to manage a background process":http://qt-project.org/forums/viewthread/31503/ only contains the .cpp files. So unless you know of another thread that i've missed, don't see how these can help someone who is just trying to do a basic example to get an understanding of the process.
This might help
"Updating GUI from QThread":http://www.qtcentre.org/wiki/index.php?title=Updating_GUI_from_QThread
Thanks Santosh Reddy, exactly what I was looking for as you can imagine QThread are a bit frustrating especially when QT documentation doesn't even have the correct method.
However just checked that link and it uses QThread as a subclass, along with this the code contains alot of errors. So still having a bit of problem with it.
Oops my post is lost !
bq. However just checked that link and it uses QThread as a subclass, along with this the code contains alot of errors. So still having a bit of problem with it.
The link has attached source files which will work in Qt 4.7
Sadly using Qt5.1 so coming up with quite a few errors. Btw is it worth me keeping with the latest version or do you suggest a different version ?
For Qt 5.x just add "#include <QtWidgets>" in the header files, and "QT += widgets" in the project file.
Cheers Santosh that fixed the main problems. Still had a few errors but they were easily fixable (specifically it details the word 'or' instead of ||) but once that was changed all worked fine (seems Qt doesn't like some of the C++ keywords. Now I have this example working starting to become alot clearer.
Jeroentjehome last edited by
There are some differences in C++11 etc. Some keywords got dropped, some got added. The OR keyword is to my knowledge not in the C++ standard, but might come from a different compiler option set. Don't blame Qt for following a standard and only the compiler will add extra options. So some keywords are dependent to your compiler.
If this post is solved, place [SOLVED] in front of your first post!
I don't blame Qt i blame the noob who thought it was a good idea to use the non standard method for creating a or (just looked this up only reason they are even in the standard is because they defined it so that keyboards without the standard operators could still use the logical operators using an alias).
So it is part of the C++ standard but they are just not implemented in Qt it seems not like it matters anyway.
"Stack Overflow explaination":http://stackoverflow.com/questions/2393673/c-and-or-not-xor-keywords
"C++ keyword reference":http://en.cppreference.com/w/cpp/keyword
Just inserting these in for the next person so they know the reasoning.