Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML Progress Bar value not getting updated



  • 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


  • Lifetime Qt Champion

    @minju Is it possible that you're blocking the event loop? Can you show your C++ code where you change the value?



  • @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


  • Lifetime Qt Champion

    @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.



  • @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!



  • @jsulm x
    The Sample C++code is below

    backend.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);

    }


  • Lifetime Qt Champion

    @minju You should rather use QTimer to send the updates regularly instead of event loop blocking loop.



  • @jsulm
    I tried timer it has the same issuse


  • Lifetime Qt Champion

    @minju Please show your code with timer



  • @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);
    }
    


  • @KroMignon
    I tried the event dispatcher the problem still exist. The progress bar only updates once


Log in to reply