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

Error connecting C++ signal to Qml Slot



  • homePopUp.qml
    @
    import QtQuick 2.0
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3
    Popup{
    id: pop
    function setSourceComp(status)
    {
    console.log("signal received",status);
    }
    Connection{
    target: home
    onDoSetSourceComp: {
    setSourceComp(status) // ReferenceError: status is not defined
    }
    ...
    }
    ...
    }
    @

    in C++ Class I have this signal
    @
    void doSetSourceComp(QSting status); // on header file
    emit doSetSourceComp("success"); // called in source file
    @

    What am i doing wrong here?



  • hi,

    @Gurubaran said in Error connecting C++ signal to Qml Slot:

    ReferenceError: status is not defined

    in homePopUp.qml you should have a ''property string status " do you have it ?



  • @LeLev
    No ,I dont have that "property string status". Actually in homePopUp.qml "status" is a function parameter (setSourceComp(status)) not a property. Still I tried it , only the reference error stopped but does not serve my purpose.

    I want to send a QString from c++ signal to Qml which will be received by this function.
    When I add debug message inside
    @
    onDoSetSourceComp: {
    console.log("Signal received") // this gets printed when the c++ signal is emitted
    setSourceComp(status) // ReferenceError: status is not defined
    }
    @

    Is there any other suggesions?



  • @Gurubaran ok,
    when you write

    
    setSourceComp(status) // ReferenceError: status is not defined  
    
    

    you are calling setSourceComp() with param status so status must be defined;
    if that string is a Q_Property of your class do :

    onDoSetSourceComp: {
    setSourceComp(home.status) // ReferenceError: status is not defined  
    }
    

  • Moderators

    @Gurubaran
    just to make sure those are typos:
    the element should be Connections, you wrote Connection
    You signal parameter is of the type QSting instead of QString

    Where does the home target come from?!
    If it is set as a context property you need to call qmlRegisterType() (or one of it's "sibling" methods) to let the QML engine know it's properties and signals properly.


Log in to reply