Is it possible to play loading animation indicator while processing event in QT GUI application?
Is it possible to play loading animation indicator while processing event in QT GUI application?
Below is my code, I have resolved to my Question .Al last i got solution with help of stack overflow team .It is possible to play animation (.gif file) on main thread if you are doing long task in worker thread.And other things is as with official doc QPixmap is does not supporting in worker thread .So i hope guys it will be help for Qt developer.
int worker::do_Work()
{
int i =0;
while (i<1000000)
{
qDebug()<<":count *i=========>"<<i;
i++;
}
qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId();
emit finished();
}
int mywidget::popup()
{
ui->label_2->setStyleSheet("background-color:rgb(85,255,127);border-radius:10px");
ui->label_2->setWindowFlags(Qt::FramelessWindowHint);
ui->label_2->setText("Please Wait..");
ui->label_2->setAutoFillBackground(true);
QMovie *movie = new QMovie(":images/loader.gif");
ui->label_2->setMovie(movie);
ui->label_2->show();
qDebug()<<"labele show";
movie->start();
myWorker = new worker;
WorkerThread = new QThread;
myWorker->moveToThread(WorkerThread);
connect(WorkerThread, SIGNAL(started()), myWorker, SLOT(do_Work()));
WorkerThread->start();
connect(myWorker, SIGNAL(finished()), ui->label_2, SLOT(close()));
return 0;
}
Can you provide a Minimal Complete, and Verifiable example?
– imoutidi
Jul 17 '17 at 9:26
I want to populate loading.gif animation file on the screen while web service call processing time .
– Subrata Das
Jul 17 '17 at 9:28
You could show the animation code, since this depends on what is shown, it may be a blocking task.
– eyllanesc
Jul 17 '17 at 9:39
We need to provide us with a code that is minimal and reproducible where the error is verified.
– eyllanesc
Jul 17 '17 at 9:40
1 Answer
1
If the main loop is busy because it is processing events, it will not update the GUI (because the GUI is managed by the main thread).
When you have long processing tasks, execute them in a different thread (or accept to freeze your GUI).
As I said, the GUI is and must be managed in the main thread :
GUI Thread and Worker Thread
As mentioned, each program has one thread when it is started. This
thread is called the "main thread" (also known as the "GUI thread" in
Qt applications). The Qt GUI must run in this thread. All widgets and
several related classes, for example QPixmap, don't work in secondary
threads. A secondary thread is commonly referred to as a "worker
thread" because it is used to offload processing work from the main
thread.
Thanks for your kind suggestion.I have checked it's working but why QPixmap is not working in worker thread?-ymoreau
– Subrata Das
16 hours ago
As it is said in the doc, you cannot perform GUI processing in the worker thread, it even uses
QPixmap as an example which don't work in secondary threads.– ymoreau
3 hours ago
QPixmap
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is animation?
– eyllanesc
Jul 17 '17 at 9:25