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:
main.cpp
@#include <QCoreApplication>
//#include <QThread>
#include "object.h"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.cpp
@#include "object.h"Object::Object(QObject *parent) :
QObject(parent)
{
}void Object::setup(QThread &thread)
{
connect(&thread,SIGNAL(started()),this,SLOT(worker()));
}void Object::worker()
{
for(int i = 0; i < 100; i++)
{
qDebug() << i;
}
}
@object.h
@#ifndef OBJECT_H
#define OBJECT_H#include <QObject>
#include <QDebug>
#include <QThread>class Object : public QObject
{
Q_OBJECT
public:
explicit Object(QObject *parent = 0);
void setup(QThread &thread);
signals:public slots:
void worker();
};#endif // OBJECT_H
@ -
Hi,
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.
What errors?
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.
-
Hi,
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!
Greetz -
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.