2022-03-25

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

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?



No comments:

Post a Comment