How do you send data from a QtWidget application Mainwindow.cpp file to a update the value of a progressbar in QML?
-
I'm new to Qt and still figuring my way around. I've started off with a course covering the basics. I am currebtly tasked to understand as much as I can in QML to see if possible to integrate it into our software which is a standard Qt Widget Application.
I basically made a dummy app for practice, and I used a QtQuickWidget inside of a UI form of a Qt Widget application. Within it I have a progressbar. I want to update the progress of the progressbar with a value sent from the Mainwindow.cpp file, which has a function there that used the QTimer class to run for a certain period of time and with every second the progress increases until reaching a 100%.
mainwindow.cpp:
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 progress = myFunction(); context->setContextProperty("_myProgress", progress); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(myFunction())); timer->start(1000); 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; if(i < total_time){ i = i + 1000; progress = ((i)*100)/total_time; qDebug()<<"Progress: "<<progress<<endl; }else{ timer->stop(); qDebug()<<"Finished..."<<endl; } return progress; }
QML file:
Rectangle{ id:mainRect; color: "black"; gradient: Gradient { GradientStop { position: 0.0; color: "black" } GradientStop { position: 0.33; color: "white" } GradientStop { position: 1.0; color: "black" } } Text { id: rectText text: ""+_myString color: "green" font.pointSize: 40 anchors.horizontalCenter: parent.horizontalCenter } ProgressBar{ id:progressbar from: 0; to:100; anchors.centerIn: parent height: 50 ColorAnimation { from: "orange" to: "green" duration: 200 } onValueChanged: _myProgress } Button{ id: control text: qsTr("Button") anchors.right: progressbar.right anchors.left: progressbar.left anchors.top : progressbar.bottom font.bold: true font.pixelSize: 20 MouseArea{ anchors.fill: control } contentItem: Text { text: control.text font: control.font opacity: enabled ? 1.0 : 0.3 color: "white" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter elide: Text.ElideRight } background: Rectangle { implicitWidth: 100 implicitHeight: 40 color: parent.down ? "darkgreen" : (parent.hovered ? "lightgreen" : "green") opacity: enabled ? 1 : 0.3 // border.color: control.down ? "#17a81a" : "#21be2b" border.width: 1 radius: 2 } } }
Now when I tried to use my "_myProgress" at the "value: " property in the ProgressBar it only send the first value in the progress and not the rest, so like th efirst 1 or 2% and that's it. In the debug console I can see the progress being calculated in my function at the C++ side but I don't know how this kind of data is supposed to be sent to the QML side. I have seen videos but usually they cover using a C++ class with its own header and cpp files and it sends data to the QML side but all this is inside a QtQuickApplication not a QtWidgetApplication.