QML Progress Bar value not getting updated
-
wrote on 23 Jan 2020, 04:54 last edited by
Hello,
I have a progress bar in qml and trying to update the value form C++. I have created a class derived from Q_OBJECT. Through Q_PROPERTY I am reading and setting the progress value. This Value is continuously updated and emitted. In QML, the value is getting updated as I checked the onValueChanghed slot but in UI the progress is not shown, the progress bar is still at 0.Can anyone help?
Thanks in advance -
Hello,
I have a progress bar in qml and trying to update the value form C++. I have created a class derived from Q_OBJECT. Through Q_PROPERTY I am reading and setting the progress value. This Value is continuously updated and emitted. In QML, the value is getting updated as I checked the onValueChanghed slot but in UI the progress is not shown, the progress bar is still at 0.Can anyone help?
Thanks in advance@minju Is it possible that you're blocking the event loop? Can you show your C++ code where you change the value?
-
@minju Is it possible that you're blocking the event loop? Can you show your C++ code where you change the value?
wrote on 23 Jan 2020, 07:40 last edited by@jsulm
Thanks for the reply
The progress value is updated in an while loop. I had used infinite loop,this lead to the progress bar from not getting updated even though the value was set. When I replaced it with a finite loop the progress bar shows the progress only once.In my while loop I am transmitting data for a definite number of times. Hence for every iteration I require the progress bar to show the progress from 0 to 1.
Kindly help
Thanks -
@jsulm
Thanks for the reply
The progress value is updated in an while loop. I had used infinite loop,this lead to the progress bar from not getting updated even though the value was set. When I replaced it with a finite loop the progress bar shows the progress only once.In my while loop I am transmitting data for a definite number of times. Hence for every iteration I require the progress bar to show the progress from 0 to 1.
Kindly help
Thanks@minju said in QML Progress Bar value not getting updated:
The progress value is updated in an while loop
That's the issue.
Do not do such things in event driven applications an GUI thread!
If you block the event loop with such an "update" loop events will not be executed (also such for GUI updates).
How to solve your problem depends on the implementation of your C++ part which I don't know as you did not post the code and did not explain what exactly you're doing there. -
@jsulm
Thanks for the reply
The progress value is updated in an while loop. I had used infinite loop,this lead to the progress bar from not getting updated even though the value was set. When I replaced it with a finite loop the progress bar shows the progress only once.In my while loop I am transmitting data for a definite number of times. Hence for every iteration I require the progress bar to show the progress from 0 to 1.
Kindly help
Thankswrote on 23 Jan 2020, 07:50 last edited by@minju said in QML Progress Bar value not getting updated:
In my while loop I am transmitting data for a definite number of times. Hence for every iteration I require the progress bar to show the progress from 0 to 1.
To work, signals/slots needs time... So if you locking the thread in a loop, there is no chance for the QEventLoop to do his job!
If the thread is the main/GUI Thread, you will also lock the hole GUI! -
@minju said in QML Progress Bar value not getting updated:
The progress value is updated in an while loop
That's the issue.
Do not do such things in event driven applications an GUI thread!
If you block the event loop with such an "update" loop events will not be executed (also such for GUI updates).
How to solve your problem depends on the implementation of your C++ part which I don't know as you did not post the code and did not explain what exactly you're doing there.wrote on 23 Jan 2020, 09:29 last edited by@jsulm x
The Sample C++code is belowbackend.cpp
Backend::Backend {
m_progress =0;
}
float Backend::progress{
return m_progress;
}
void Backend::setprogress(const float & data)
{
if(data == m_progress)
return;m_progress=data;
emit progressChanged() ;
}
void Backend::startTxn() {
for(int i=0;i<10;i++) {
setprogress (.2);
//some processing
setprogress (.4);
setprogress (.6);
setprogress (1);}
-
@jsulm x
The Sample C++code is belowbackend.cpp
Backend::Backend {
m_progress =0;
}
float Backend::progress{
return m_progress;
}
void Backend::setprogress(const float & data)
{
if(data == m_progress)
return;m_progress=data;
emit progressChanged() ;
}
void Backend::startTxn() {
for(int i=0;i<10;i++) {
setprogress (.2);
//some processing
setprogress (.4);
setprogress (.6);
setprogress (1);}
@minju You should rather use QTimer to send the updates regularly instead of event loop blocking loop.
-
@minju You should rather use QTimer to send the updates regularly instead of event loop blocking loop.
-
@minju Please show your code with timer
-
@jsulm x
The Sample C++code is belowbackend.cpp
Backend::Backend {
m_progress =0;
}
float Backend::progress{
return m_progress;
}
void Backend::setprogress(const float & data)
{
if(data == m_progress)
return;m_progress=data;
emit progressChanged() ;
}
void Backend::startTxn() {
for(int i=0;i<10;i++) {
setprogress (.2);
//some processing
setprogress (.4);
setprogress (.6);
setprogress (1);}
wrote on 23 Jan 2020, 10:47 last edited by@minju said in QML Progress Bar value not getting updated:
void Backend::startTxn() {
for(int i=0;i<10;i++) {
setprogress (.2);
//some processing
setprogress (.4);
setprogress (.6);
setprogress (1);
}This function will also lock the event loop of the QThread used by Backend.
You may try this:#include <QAbstractEventDispatcher> ... void Backend::setprogress(const float & data) { if(data == m_progress) return; m_progress=data; emit progressChanged() ; this->thread()->eventDispatcher()->processEvents(QEventLoop::AllEvents); }
-
@minju said in QML Progress Bar value not getting updated:
void Backend::startTxn() {
for(int i=0;i<10;i++) {
setprogress (.2);
//some processing
setprogress (.4);
setprogress (.6);
setprogress (1);
}This function will also lock the event loop of the QThread used by Backend.
You may try this:#include <QAbstractEventDispatcher> ... void Backend::setprogress(const float & data) { if(data == m_progress) return; m_progress=data; emit progressChanged() ; this->thread()->eventDispatcher()->processEvents(QEventLoop::AllEvents); }
wrote on 24 Jan 2020, 03:52 last edited by@KroMignon
I tried the event dispatcher the problem still exist. The progress bar only updates once
1/11