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

qml dummy property



  • Hello,
    I'm interesting into qmlscene.
    In project I use:

    engine.rootContext()->setContextProperty("PP_IP", QVariant("192.168.100.195"));
    

    to set device IP adress.

    And in QML:

        Component.onCompleted: {
            console.log("Test from example: "+PP_IP.time) //this is for debug
           console.log("Test: "+PP_IP) //this is what I need
        }
    

    Now I want use dummy data to set PP_IP when I running qml in qmlscene.

    So I created file dummydata/PP_IP.qml
    In this file I have

    import QtQuick 2.3
    QtObject { property int time: 54321 }
    

    Like in example , OK it is working.
    But I need print variable PP_IP not PP_IP.time.
    What I have to change in "dummydata/PP_IP.qml" ??
    Thank you very much



  • @poucz said in qml dummy property:

    Component.onCompleted: {
        console.log("Test from example: "+PP_IP.time) //this is for debug
       console.log("Test: "+PP_IP) //this is what I need
    }
    

    You cannot access the object in 'PP_IP.qml' file as PP_IP.time.
    You must create an instance first, e.g.

        PP_IP {
            id: pp_ip
        }
    

    Then use the id to print the time:

    console.debug(tt_ip.time)
    

    As for giving the context variable PP_IP the same name as your QML type defined by 'PP_IP.qml', that won't work either.
    The context variable's name gets masked by the Type name.

    If you want to use both, give them different names.



  • Thank for your reply.

    Sorry maybe I describe my problem wrong, or I don't understand your answer.

    I set contex propertyin C++:

    engine.rootContext()->setContextProperty("PP_IP", QVariant("192.168.100.195"));
    

    When I run my program, and show "mypage.qml" - everythink is OK - console log IP "192.168.100.195" .

    Now when I use qmlscene to show "mypage.qml" , the property PP_IP is not set (because C++ code not run).
    So I want set these variable by dummy data. How can I do this?

    I need print IP adress 192.168.100.195 - when I run mypage.qml from qmlscene.
    Can you post dummydata/exmaple.qml for me?

    Thank you for your patience!!

    //mypage.qml
    Rectangle{
        Component.onCompleted: {
           console.log("Test: "+PP_IP) //print IP adress 
        }
    }
    


  • This post is deleted!

Log in to reply