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

Proper access of buttons from ButtonGroup?



  • Hi,
    in an application I create buttons and assign them to a ButtonGroup dynamically.

    In the ButtonGroup I would like use and modify properties of the button clicked.
    This works in the following MWE.

    However, the usage of "button.text" throws the message:
    Parameter "button" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead.

    What is the proper way to fix this?

    MWE:

    import QtQuick 6
    import QtQuick.Window 2.15
    import QtQuick.Controls 6
    import QtQuick.Layouts 1.15
    
    ApplicationWindow {
        id: window
        width: 450
        height: 300
        visible: true
    
    ButtonGroup {
        id:btnGroup
    
        onClicked: {
            console.log("clicked:", button.text) //Throws message
        }
    }
    
    GridLayout  {
        columns: 2
        anchors.fill: parent
    
        // DUMMY BUTTONS, IN REALITY DYNAMICALLY CREATED
        Custom_btn {
            text: qsTr("Dummy1")
            ButtonGroup.group: btnGroup 
        }
        Custom_btn {
            text: qsTr("Dummy2")
            ButtonGroup.group: btnGroup 
        }
    
    }
    
    }
    

    Thanks!



  • @robro Use

        ButtonGroup {
            id: btnGroup
            onClicked: function(button) {
                console.log("clicked:", button.text);
            }
        }
    

Log in to reply