PDA

View Full Version : Update GUI from another thread



Anne
14th July 2010, 12:24
Hi Everyone!

I'm struggling with the following problem for quite some time. My application runs heavy mathematical equations based on input files. The actual work is done within a separate thread. This way the GUI remains responsive. Everything works great, but I just can't figure out how to update the GUI from the separate thread. While processing the equations I want the GUI to show which equation is currently processed.

I just recently started using QT. This because I need to develop a application that runs on both Windows and Mac. The concept of slots is hard to me. I have developed many applications using Xcode and Cocoa. In this environment I can create complicated multithreading applications (that continuously update the GUI) without any problem. My C++ knowledge is average.

It drives me crazy. I spend to much time figuring this out. And still no result.

To the point:
Who can create an sample project for me?

The sample must contain:
- GUI with one text-box / button
- Clicking the button starts a new thread
- The new thread executes a loop 5 times.
- The loop simulates a heavy process (for example by using 1-second sleep)
- Every time the loop starts the GUI is updated by appending a line to the textbox.

I'm willing to pay for your time. I definitely need some help here. Please keep the sample as simple as possible. This way i can understand how it works.

(A little desperate)
Kind regards,
Anne

Lykurg
14th July 2010, 13:26
How does your thread look like? Add in *.h file
Q_SIGNALS:
void currentEquitation(QString);
In the cpp, where you calculate a new equation simple do
Q_EMIT currentEquitation("Equitation foo");
And then in your GUI class where you start your thread (assuming it is called thread and you have a QLabel called label):
QObject::connect(thread, SIGNAL(currentEquitation(QString)), label, SLOT(setText(QString)));
That's all.

^NyAw^
14th July 2010, 13:46
Hi,

Hope this simple example will help you.

Lykurg
14th July 2010, 13:54
You don't have to subclass QThread, you can simply use existing classes for compilation and use them "directly":
#include <QtGui>


class calc : public QObject
{
Q_OBJECT

public Q_SLOTS:
void doCalc()
{
for (int i = 1; i < 25; ++i)
{
Q_EMIT message(QString::number(i));
QTime t = QTime::currentTime();
t = t.addSecs(2);
while (t > QTime::currentTime()) {}
}
}

Q_SIGNALS:
void message(QString);
};


int main(int argv, char** args)
{
QApplication app(argv, args);

// GUI
QWidget w;
QPushButton* b = new QPushButton("start", &w);
QLabel* l = new QLabel(&w);
QLineEdit* le = new QLineEdit(&w);
QVBoxLayout* la = new QVBoxLayout();
la->addWidget(l);
la->addWidget(b);
la->addWidget(le);
w.setLayout(la);
w.show();

// Thread
calc c;
QThread t;
c.moveToThread(&t);
t.start();

// Connections
QObject::connect(&c, SIGNAL(message(QString)), l, SLOT(setText(QString)));
QObject::connect(b, SIGNAL(clicked()), &c, SLOT(doCalc()));

return app.exec();
}

#include "main.moc"

Lykurg
14th July 2010, 14:00
I'm willing to pay for your time.
If you want to offer a job, even if it is a small one, please use your "Jobs" forum.

Nevertheless, if you found the answers usefull, nobody will stop you if you spend some money to QtCentre.org or KDE e.V. or or or... :)

Anne
14th July 2010, 14:27
Dear Lykurg and ^NyAw^,

Wow, is it just that simple?! QT really amazes me.

You really helped me out here, everything is perfectly clear right now. I was thinking too complicated. In Xcode / Cocoa it's actually much harder to do exactly this. If you ever have any Xcode / Cocoa related question, please contact me!

Generally I never ask for help. I want to figure out things like these myself. In the long run this is the best way to learn a new language in my opinion. But in this case I was completely stuck.

I did not really intend to offer a job, was just asking for an example. Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.

All your help is greatly appreciated,
Kind regards,
Anne

wysota
14th July 2010, 15:14
Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.
Qt Centre paypal account is foundation at qtcentre.org.

Anne
14th July 2010, 15:24
@wysota
Thanks for the address!

@^NyAw^ and Others
I tried to open your sample project, but the folder does not contain any .pro files.
The only files I see are .h, .cpp, .ui and .qrc.
How can I open such an project directly with QT? The .qrc is not recognized?
(Of cours I now just directly used the .cpp and .h files, but was just curious)

Lykurg
14th July 2010, 15:32
You can run "qmake -project" inside that folder. Then Qt is generating a pro file for you.

^NyAw^
14th July 2010, 16:08
Hi,



@^NyAw^ and Others
I tried to open your sample project, but the folder does not contain any .pro files.
The only files I see are .h, .cpp, .ui and .qrc.
How can I open such an project directly with QT? The .qrc is not recognized?
(Of cours I now just directly used the .cpp and .h files, but was just curious)

I just don't added the pro file because I'm using Visual Studio on Windows and I'm not using the pro file, but there is an option to create the pro file that then I can attach.
Just use "qmake -project" as Lykurg said to create the "pro" file and then call "make" to compile the project.



Generally I never ask for help

It's a good idea to learn by yourself, but sometimes the examples don't show what you are expecting or you are not able really how something works. Then, ask the forum! (but first of all try to find similar posts on it).