Update GUI from another thread
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
Re: Update GUI from another thread
How does your thread look like? Add in *.h file In the cpp, where you calculate a new equation simple do
Code:
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):That's all.
1 Attachment(s)
Re: Update GUI from another thread
Hi,
Hope this simple example will help you.
Re: Update GUI from another thread
You don't have to subclass QThread, you can simply use existing classes for compilation and use them "directly":
Code:
#include <QtGui>
{
Q_OBJECT
public Q_SLOTS:
void doCalc()
{
for (int i = 1; i < 25; ++i)
{
Q_EMIT message
(QString::number(i
));
t = t.addSecs(2);
while (t >
QTime::currentTime()) {} }
}
Q_SIGNALS:
};
int main(int argv, char** args)
{
// GUI
la->addWidget(l);
la->addWidget(b);
la->addWidget(le);
w.setLayout(la);
w.show();
// Thread
calc c;
c.moveToThread(&t);
t.start();
// Connections
QObject::connect(b,
SIGNAL(clicked
()),
&c,
SLOT(doCalc
()));
return app.exec();
}
#include "main.moc"
Re: Update GUI from another thread
Quote:
Originally Posted by
Anne
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... :)
Re: Update GUI from another thread
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
Re: Update GUI from another thread
Quote:
Originally Posted by
Anne
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.
Re: Update GUI from another thread
@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)
Re: Update GUI from another thread
You can run "qmake -project" inside that folder. Then Qt is generating a pro file for you.
Re: Update GUI from another thread
Hi,
Quote:
Originally Posted by
Anne
@^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.
Quote:
Originally Posted by
Anne
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).