Niranj Patel
Niranj Patel

Reputation: 33258

Add dynamically button with click event

I am implemeting Multiple add button programmatically. I sucessfully add button but I can't able get click event for all button. I am always getting click on last added button.

I want separately click event for all button.

I am using this code for add button.

ComponentDefinition {
            id: mComponentDefinitionSubmitButton
            Button {
                id: mButtonID
                horizontalAlignment: HorizontalAlignment.Center
                onClicked: {
                    //My Click code. Always detect last button.
                }
            }
        }

var mButton = mComponentDefinitionSubmitButton.createObject();
mButton.text = qsTr(title)
mContainerButton.add(mButton)

Upvotes: 0

Views: 1135

Answers (1)

Niranj Patel
Niranj Patel

Reputation: 33258

I done with signal..

function checkClick(button)
    {
        console.debug("click..."+ button);
    }
    attachedObjects: [
        ComponentDefinition {
            id: mComponentDefinitionSubmitButton

            Button {
                id: mButtonID
                signal click(variant text);
                horizontalAlignment: HorizontalAlignment.Center
                onClicked: {
                    click(mButtonID.text);
                }
            }
        }
    ]

----------------------------------------------------------------
var mButton = mComponentDefinitionSubmitButton.createObject();
mButton.text = qsTr("Button");
mButton.click.connect(checkClick);
btnContainer.add(mButton);

Upvotes: 3

Related Questions