Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Assigning a Value From .cpp file to .qml file



  • 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



  • @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


Log in to reply