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

QML receives a C++ object



  • Hi all,
    in my application I have a QML front-end where the user types a code.
    This code is checked against a list of values read on a database using C++ code.
    Now I'm able to recall the Q_INVOKABLE C++ function to query the database.
    The function returns an object containing the result and QML code should check the data and decide how to go on.
    I'm not able to use this object.

    var r = ds.checkCode( TipoPrenotazione.TIPO_RITIRO, edtCodicePrenotazione.text)
    console.log("ret: "+r)
    if (r===null)
             txtMsg.text = "Prenotazione not found"
    else {
             console.log("Trovata!", r)
             txtMsg.text = "Prenotazione found !"
             console.log(r._cassetto) //??
    }
    

    In the debug window I see:

    [DataSource] p: Prenotazione(0x8098b118)
    qml: ret: Prenotazione(0x8098b118)
    qml: Trovata! Prenotazione(0x8098b118)
    qml: undefined
    

    So the object is correct but how to access it ?

    I'm wondering if this flow is correct ...
    Thanks


  • Moderators

    @SteMMo said in QML receives a C++ object:

    So the object is correct but how to access it ?

    Is _cassetto a Q_PROPERTY? You can only access Q_INVOKABLE methods and Q_PROPERTY values from QML.


  • Moderators

    @SteMMo said in QML receives a C++ object:

    console.log(r._cassetto) //??

    i am not sure if it also applies to properties of QObjects, but for QML defined properties the property name MUST begin with a lowercase letter:

    Property names must begin with a lower case letter and can only contain letters, numbers and underscores



  • @sierdzio
    yes it is.
    But the key is: when I wrote the QML code and I use the r object, I cannot see its members.
    My guess is that the QML editor is not able to know the type of the returned object and I cannot define the r variable as Prenotazione.
    The runtime pointer is correct but the code is not able to locate 'cassetto'.
    Now I defined it as:

    Q_PROPERTY(int cassetto MEMBER _cassetto READ cassetto)
    

    and added

    int cassetto() { return _cassetto; };
    

    thanks


  • Moderators

    Q_PROPERTY(int cassetto MEMBER _cassetto READ cassetto)
                   ^ this is your property name
    

    It does not matter how your member variable is named, do not use it in QML! What QML sees is the property name (first string after type in Q_PROPERTY invokation).


Log in to reply