Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do you send data from a QtWidget application Mainwindow.cpp file to a update the value of a progressbar in QML?

How do you send data from a QtWidget application Mainwindow.cpp file to a update the value of a progressbar in QML?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 381 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kokoy
    wrote on last edited by
    #1

    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.

    artwawA 1 Reply Last reply
    0
    • K Kokoy

      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.

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @Kokoy https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html all rather well explained here.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kokoy
        wrote on last edited by
        #3

        appreciate your response

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved