QML property is not changing from inside the same file



  • Hello,

    I'm having an issue with reading the correct value of a QML property from C++.

    main.qml

    ApplicationWindow {
        id: applicationWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Tabs")
    
        property int valuef1: page1.valuef33
    .
    .
    .
           Page1{
                id:page1
    
            }
    .
    .
    .
    }
    

    "other_files_reaching_each_other's_propeperty_the same_way".qml

    Page1Form {
    
        property int valuef33: freqWindow.valuef44
    
    }
    

    ButtonLine.qml

    ButtonLineForm {
    
     property int  valuef55: 558
    
        mArea{        
            onClicked: {
                console.log("test:", valuef55)
            }
        }
        mArea1{
            onClicked: {
                valuef55 = 4
            }
        }
        mArea2{
            onClicked: {
                valuef55 = 3
            }
        }
    .
    .
    .
    }
    

    In C++ I'm reaching the valuef1 property of main.qml:

    qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
    

    All the properties are connected, so I'm reading "Property value: 558". But when I click on the mArea1, and then try to read the property value, which should be equal to 4 after the click, it shows 558 again. Clicking mArea after mArea1 and printing valuef55 in the console shows it's equal to 4.

    What should I do to get the right value in C++? Is this the correct way of getting a value from QML item, and if it's not, how to do it in better way?


  • Qt Champions 2017

    Are you getting any error ? What is the value are you getting ?
    object what you passed to QQmlProperty is it the correct object where property exist in qml ?
    Now why do you want to access the value in C++ ? Do you want to pass the value some where ?



  • I'm not getting any error, I'm getting wrong value. I have 5 buttons which pressed should correspondingly change the property value of "valuef55" in ButtonLine.qml. When I define "valuef55" I'm giving value "558" , and that's what I'm reading from C++, regardless what button has been pressed. The idea is to read from C++ which one of the 5 buttons has been pressed.



  • @krokstr said in QML property is not changing from inside the same file:

    qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();

    Since your question is about getting the value property valuef1 of applicationWindow from C++, and since that value never seems to change, have you checked that it effectively changes in QML when you press your buttons in QML ?

    e.g. does this work:

    applicationWindow {
         ...
         property int valuef1: page1.valuef33
    
         onValuef1Changed : {
              console.debug("valuef1 changed: " + valuef1)
         }
    }
    
    

    Since object seems to point to an instance of ApplicationWindow, can you show how you obtain that object pointer to it?

    Furthermore, can you show us where in your C++ code you try to use this debug statement:

    qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
    


  • Okay, I've just tried that the "valuef1" changes all the way to "applicationWindow", so the problem is in my C++ code.

    main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
    
    
        my_class My_class;
        engine.rootContext()->setContextProperty("My_class", &My_class);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    my_class.cpp

    my_class::my_class(QObject *parent) : QObject(parent)
    {
        object = NULL;
        engine = new QQmlEngine;
        component = new QQmlComponent(engine, "qrc:/main.qml");
        object = component->create();
    
    
    }
    my_class::~my_class()
    {
        if(object)
        {
            delete object;
            object = NULL;
        }
        if(engine)
        {
            delete engine;
            engine = NULL;
        }
        if(component)
        {
            delete component;
            component = NULL;
        }
    }
    
    void my_class::connection_test()
    {
        qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
    }
    

    I know that's not the correct way of doing it, but I've tried many examples and haven't managed it to run. So I'm just giving you the last one I've left in my code.

    connection_test() is a slot which is called when a button in QML is pressed.

        mouseArea{
            onClicked: My_class.connection_test()
        }
    

    With this configuration, when I call "connection_test()", it is giving me an error "ReferenceError: My_class is not defined". So I believe I'm doing a deadlock, but I don't know how to fix it. In many of my tries, it started printing the qDebug in "connection_test()", but after that I had to push the back button of my device to continue, otherwise the app freezes.



  • Hi @krokstr
    Have a look at this:
    https://stackoverflow.com/questions/23177839/how-can-i-access-my-window-object-properties-from-c-while-using-qqmlapplicatio
    It clearly shows what you need to do to access your main QML window object.



  • Thank you, that link was the key! I made it!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.