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

Cannot figure out Connections{} for signal



  • import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        signal testSignal()
    
        Rectangle {
            id:testRec
            property string colorVar: "blue"
            width:400
            height:400
            color: colorVar
            Connections {
                target: testRec
                onTestSignal: colorVar = "red" //Cannot assign to non-existent property "onTestSignal"
            }
        }
        Button{
            width:100
            height:100
            text: "Button"
            onClicked: {
                console.log("button pressed")
                testSignal()
            }
        }
    }
    

    Hi Guys

    For the life of me I can't figure out what I might be doing wrong here.
    I can't get on<signal> to work at all. I've tried numerous examples and always get this error: Cannot assign to non-existent property "onTestSignal"

    I've tried just adding on<signal> without the Connections{}
    Also with and without target
    Also ontestSignal: and onTestsignal:

    If I type on<signal> without the Connections{} it always has a red line under it.. error invalid property name, and doesn't run at all. with Connections{} it runs and gives an error without the button being pressed.

    Is there maybe some import I am missing?

    Thanks



  • Hi,

    @ButterBoubon said in Cannot figure out Connections{} for signal:

    I can't figure out what I might be doing wrong here

    The Connections has testRec as target, so the signal must be declared inside testRec

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Connections {
            target: testRec
           // onTestSignal: colorVar = "red" //Cannot assign to non-existent property "onTestSignal"
                onTestSignal: testRec.color="red"
        }
            Rectangle {
                id:testRec
                signal testSignal()
               // property string colorVar: "blue" // Invalid write to global property
                width:400
                height:400
                color: "green"
    
            }
            Button{
                width:100
                height:100
                text: "Button"
                onClicked: {
                    console.log("button pressed")
                    testRec.testSignal()
                }
            }
    }
    

    More flexible approch is to declare the testRec in separat .qml Component file



  • Thank you!

    I separate all my items into separate qml files. I just put together a quick thing not to overload the question with too much info. It makes things so much more less confusing.


Log in to reply