Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Assigning a Value From .cpp file to .qml file

Assigning a Value From .cpp file to .qml file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 417 Views
  • 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.
  • V Offline
    V Offline
    Vinay Kumar pusuluri
    wrote on last edited by
    #1

    import QtQuick 2.2

    Item {
    id: valueSource
    property real kph: 0
    property real rpm: 1
    property real fuel: 0.85
    property int someNumber: 40
    // property var aNumber: 100
    property string gear: {
    var g;
    var t=20;
    if (kph == 0) {
    return "P";
    }
    if (kph < 30) {
    return "1";
    }
    if (kph < 50) {
    return "2";
    }
    if (kph < 80) {
    return "3";
    }
    if (kph < 120) {
    return "4";
    }
    if (kph < 160) {
    return "5";
    }
    }
    property int turnSignal: gear == "P" && !start ? randomDirection() : -1
    property real temperature: 0.6
    property bool start: true

    function myQmlFunction(msg) {
           console.log("Got message:", msg)
           return "some return value"
       }
    function myQmlFunction1(msg) {
           console.log("Got message2:", msg)
            valueSource.someNumber =msg
        console.log("valueSource.someNumber Value is:",valueSource.someNumber)
        return valueSource.someNumber
       }
    function randomDirection() {
        return Math.random() > 0.5 ? Qt.LeftArrow : Qt.RightArrow;
    }
    
    SequentialAnimation {
        running: true
        loops: 1
    
        
        PauseAnimation {
            duration:1000
        }
    
        PropertyAction {
            target: valueSource
            property: "start"
            value: false
        }
    
        SequentialAnimation {
            loops: Animation.Infinite
            ParallelAnimation {
                NumberAnimation {
                    
                    target: valueSource
                    property: "kph"
                    easing.type: Easing.InOutSine
                    from:valueSource.someNumber
                   
                    to: 60
                    duration:2000
                }
                NumberAnimation {
                    target: valueSource
                    property: "rpm"
                    easing.type: Easing.InOutSine
                    from: 1
                    to: 6.1
                    duration: 3000
                }
            }
        }
    
    }
    

    }
    this is my MainWindow.cpp file
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include"QDebug"
    #include <QQmlEngine>
    #include"QtQuickWidgets/QQuickWidget"
    #include"QQuickView"
    #include"QString"
    #include"form.h"
    #include<frnm.cpp>
    QQuickWidget *quick;
    QObject *item;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

              ui->quickWidget->setSource(QUrl("qrc:/qml/dashboard.qml"));
    

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

    void MainWindow::on_pushButton_2_clicked()
    {
    QString str=ui->lineEdit->text();
    int s1=str.toInt();

    QQmlEngine engine;
    QQmlComponent component(&engine,QUrl("qrc:/qml/ValueSource.qml"));
    QObject *object = component.create();
    
    qDebug() << "Property value:" << QQmlProperty::read(object, "someNumber").toInt();
    QQmlProperty::write(object, "someNumber", s1);
    qDebug() << "Property value:" << object->property("someNumber").toInt();
    object->setProperty("someNumber", s1);
    QQmlEngine engine1;
    QQmlComponent component1(&engine1, QUrl("qrc:/qml/ValueSource.qml"));
    QObject *object1 = component1.create();
    
    QVariant returnedValue;
    QVariant msg = "Hello from C++";
    QVariant v(s1);
    QMetaObject::invokeMethod(object1, "myQmlFunction",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, msg));
    qDebug() << "QML function returned:" << returnedValue.toString();
    QMetaObject::invokeMethod(object1, "myQmlFunction1",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, v.toInt()));
    
    
    qDebug() << "QML function returned2:" << returnedValue.toInt();
    delete object;
    

    }

    here What ever I am Taking the Value From LineEdit I am Sending to the .qml file ,
    In .Qml File also getting but this value should not be assigning...
    in .QML file my UserDefined function i have written(i.e. function myQmlFunction1(msg)) In This I am Changing this (property int someNumber:) value. but it is not effecting the SequentialAnimation{

    }.I am using this (property int someNumber: ) in SequentialAnimation{

    } .can u guide to me how we can use that variable in SequentialAnimation.
    thanks

    Pablo J. RoginaP 1 Reply Last reply
    0
    • V Vinay Kumar pusuluri

      import QtQuick 2.2

      Item {
      id: valueSource
      property real kph: 0
      property real rpm: 1
      property real fuel: 0.85
      property int someNumber: 40
      // property var aNumber: 100
      property string gear: {
      var g;
      var t=20;
      if (kph == 0) {
      return "P";
      }
      if (kph < 30) {
      return "1";
      }
      if (kph < 50) {
      return "2";
      }
      if (kph < 80) {
      return "3";
      }
      if (kph < 120) {
      return "4";
      }
      if (kph < 160) {
      return "5";
      }
      }
      property int turnSignal: gear == "P" && !start ? randomDirection() : -1
      property real temperature: 0.6
      property bool start: true

      function myQmlFunction(msg) {
             console.log("Got message:", msg)
             return "some return value"
         }
      function myQmlFunction1(msg) {
             console.log("Got message2:", msg)
              valueSource.someNumber =msg
          console.log("valueSource.someNumber Value is:",valueSource.someNumber)
          return valueSource.someNumber
         }
      function randomDirection() {
          return Math.random() > 0.5 ? Qt.LeftArrow : Qt.RightArrow;
      }
      
      SequentialAnimation {
          running: true
          loops: 1
      
          
          PauseAnimation {
              duration:1000
          }
      
          PropertyAction {
              target: valueSource
              property: "start"
              value: false
          }
      
          SequentialAnimation {
              loops: Animation.Infinite
              ParallelAnimation {
                  NumberAnimation {
                      
                      target: valueSource
                      property: "kph"
                      easing.type: Easing.InOutSine
                      from:valueSource.someNumber
                     
                      to: 60
                      duration:2000
                  }
                  NumberAnimation {
                      target: valueSource
                      property: "rpm"
                      easing.type: Easing.InOutSine
                      from: 1
                      to: 6.1
                      duration: 3000
                  }
              }
          }
      
      }
      

      }
      this is my MainWindow.cpp file
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include"QDebug"
      #include <QQmlEngine>
      #include"QtQuickWidgets/QQuickWidget"
      #include"QQuickView"
      #include"QString"
      #include"form.h"
      #include<frnm.cpp>
      QQuickWidget *quick;
      QObject *item;
      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

                ui->quickWidget->setSource(QUrl("qrc:/qml/dashboard.qml"));
      

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

      void MainWindow::on_pushButton_2_clicked()
      {
      QString str=ui->lineEdit->text();
      int s1=str.toInt();

      QQmlEngine engine;
      QQmlComponent component(&engine,QUrl("qrc:/qml/ValueSource.qml"));
      QObject *object = component.create();
      
      qDebug() << "Property value:" << QQmlProperty::read(object, "someNumber").toInt();
      QQmlProperty::write(object, "someNumber", s1);
      qDebug() << "Property value:" << object->property("someNumber").toInt();
      object->setProperty("someNumber", s1);
      QQmlEngine engine1;
      QQmlComponent component1(&engine1, QUrl("qrc:/qml/ValueSource.qml"));
      QObject *object1 = component1.create();
      
      QVariant returnedValue;
      QVariant msg = "Hello from C++";
      QVariant v(s1);
      QMetaObject::invokeMethod(object1, "myQmlFunction",
              Q_RETURN_ARG(QVariant, returnedValue),
              Q_ARG(QVariant, msg));
      qDebug() << "QML function returned:" << returnedValue.toString();
      QMetaObject::invokeMethod(object1, "myQmlFunction1",
              Q_RETURN_ARG(QVariant, returnedValue),
              Q_ARG(QVariant, v.toInt()));
      
      
      qDebug() << "QML function returned2:" << returnedValue.toInt();
      delete object;
      

      }

      here What ever I am Taking the Value From LineEdit I am Sending to the .qml file ,
      In .Qml File also getting but this value should not be assigning...
      in .QML file my UserDefined function i have written(i.e. function myQmlFunction1(msg)) In This I am Changing this (property int someNumber:) value. but it is not effecting the SequentialAnimation{

      }.I am using this (property int someNumber: ) in SequentialAnimation{

      } .can u guide to me how we can use that variable in SequentialAnimation.
      thanks

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @Vinay-Kumar-pusuluri a couple of things:

      1. you may want to take a look at this article regarding how to integrate C++ and QML; this is QML will be notified when a C++ value changes without writing lots of code, look at the example there.
      2. little off-topic, but you're using the QML in a procedural way while it's expected to be used in a declarative way:

      QML is a declarative language that allows user interfaces to be described in terms of their visual components and how they interact and relate with one another

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      • Login

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