Home > Blockchain >  How to update a progress Bar in QML by calculating the countdown on the C side in the QTWidget?
How to update a progress Bar in QML by calculating the countdown on the C side in the QTWidget?

Time:03-25

I basically want to send the progress from the c side to update the value of the ProgressBar on the QML size.

I am trying to integrate QML into a widget application. The thing is I have my mainwindwo.cpp file like this:

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QQmlContext *context = ui->quickWidget->rootContext();
    QString title = "Progress Bar App";
    int progress = 0;
    context->setContextProperty("_myString",title); //Set Text in QML that says Hello World

    timer = new QTimer(this);


    connect(timer, SIGNAL(timeout()), this, SLOT(myFunction()));
    timer->start(1000);

    progress = myFunction();
    context->setContextProperty("_myProgress", progress);
    if(progress == 100.0){
        timer->stop();
    }

    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
    ui->quickWidget->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::myFunction(){
    //Calculate progress
    static uint16_t total_time = 50000;
    static uint16_t progress = 0;
    static uint16_t i = 0;
//    for(double i=0; i<=total_time; i=i 1000){

//        progress = ((i)*100)/total_time;
//        qDebug()<<"Progress: "<<progress<<endl;

//    }

    if(i < total_time){
        i = i   1000;
                progress = ((i)*100)/total_time;
                qDebug()<<"Progress: "<<progress<<endl;
    }else{
        qDebug()<<"Finished: "<<progress<<endl;
    }

    return progress;

}

I want to send the progress to calculated here to the ProgressBar in QML side but I can't seem to get it to work.

My question is if you typically create a C class with a header and cpp files, how do you do the samething but instead use things in a QTWidget or the Mainwindow.cpp file and send data continuously from it to the QML file to update a ProgressBar?

CodePudding user response:

You have to create a class that inherits from QObject and is a bridge between C and QML:

#ifndef PROGRESSBRIDGE_H
#define PROGRESSBRIDGE_H

#include <QObject>

class ProgressBridge : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
public:
    explicit ProgressBridge(QObject *parent = nullptr)
        : QObject{parent}, m_progress{0}
    {}
    int progress() const{
        return m_progress;
    }
    void setProgress(int newProgress){
        if (m_progress == newProgress)
            return;
        m_progress = newProgress;
        emit progressChanged();
    }
signals:
    void progressChanged();
private:
    int m_progress;
};

#endif // PROGRESSBRIDGE_H

mainwindow.h

ProgressBridge progress_bridge;

mainwindow.cpp

    context->setContextProperty("progressBridge", &progress_bridge);
    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));
int MainWindow::myFunction(){
    // TODO
    progress_bridge.setProgress(progress);
}

And in QML you do use Connections

ProgressBar{
    id: progressbar
    from: 0.0
    to: 100.0
}

Connections{
    target: progressBridge
    function onProgressChanged(){
        progressbar.value = progressBridge.progress
    }
}
  • Related