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

Not able to handle Signals



  • Hello all,
    I am newbie to Qt and trying to load different pages with loader. But, am getting following errors while running

    qrc:/ScreenPage.qml:10:5: QML Connections: Detected function "onSampleButtonPressed" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.

    qrc:/HomeScreen.qml:28: TypeError: Property 'sampleButtonPressed' of object [object Object] is not a function

    I think, i am handling the signals and its handlers properly. But, dont know where am doing wrong

    My code looks like this
    main.qml
    Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    id: rootWindowID

    ScreenPage {
        id:applicationWrapper
        anchors.centerIn: parent
    }
    

    }

    ScreenPage.qml
    Item {
    id:screenpage
    width: 640
    height: 480

    Connections {
        target: ScreenConfiguration
    
        function onSampleButtonPressed(pressed) {
            if(pressed === ScreenConfiguration.Pagenumbers.Page1)
            {
                //pageLoader.source = page1Component
                console.log("Page1")
            }
            else if(pressed === ScreenConfiguration.Pagenumbers.Page2)
            {
                //pageLoader.source = page2Component
                console.log("Page2")
            }
        }
    }
    
    /*Home Screen Component*/
    Component{
       id:homeScreenComponent
    
       //QML filenames should always start with Capital letters
        HomeScreen{  }
    }
    
    Loader{
        id:pageLoader
        anchors.fill: screenpage
        sourceComponent: homeScreenComponent // default and start page
    }
    

    }

    HomeScreen.qml
    Item {
    id: homescreenId
    width: 640
    height: 480

    Text {
        id: txtid1
        text: qsTr("Home Screen")
        width: parent.width
        height: 50
        anchors.left: parent.left
        font.pixelSize: 20
    }
    
    Button {
        id: buttonId1
        text: "Button1"
        width: 100
        height: 50
        anchors.top: txtid1.bottom
    
        onClicked: {
            console.log("Button1 Clicked")
            ScreenConfiguration.sampleButtonPressed(ScreenConfiguration.Pagenumbers.Page1)
        }
    }
    
    Button {
        id: buttonId2
        text: "Button2"
        width: 100
        height: 50
        anchors.top: txtid1.bottom
        anchors.left: buttonId1.right
    
        onClicked: {
            console.log("Button2 Clicked")
            ScreenConfiguration.sampleButtonPressed(ScreenConfiguration.Pagenumbers.Page2)
        }
    }
    

    }

    ScreenConfiguration.qml
    QtObject {

    signal sampleButtonPressed(int pressed)
    
    enum Pagenumbers {
        None = 0,
        Page1,
        Page2
    }
    

    }



  • @Praveen-Illa said in Not able to handle Signals:

    qrc:/ScreenPage.qml:10:5: QML Connections: Detected function "onSampleButtonPressed" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.

    that is because of this

    Connections {
        target: ScreenConfiguration  //<<  target should be an object
    

    example :

    ScreenConfiguration { id: myConfig} // create an object 
    Connections {
        target: myConfig
    

    then HomeScreen.qml:28: TypeError: Property 'sampleButtonPressed' of object [object Object] is not a function
    same thing; you need an instance/object to access the signals/attributes

    onClicked: {
                ScreenConfiguration.sampleButtonPressed(ScreenConfiguration.Pagenumbers.Page1)
    

    since you use ScreenConfiguration in several qml components you would have to create ScreenConfigurationobjects multiple times..
    I would move the signal somewhere else and make ScreenConfiguration.qml singleton



  • @LeLev
    Thanks for your quick response, it works


Log in to reply