Manas Prakash
Manas Prakash

Reputation: 33

connect dynamically created buttons in qt5

I have a scenario where I am asking the user for a number between 1 and 10 and creating that number of buttons of the type QPushButton. I then want to create a function such that when I click the button the number on the button gets printed.

Upvotes: 0

Views: 117

Answers (1)

JarMan
JarMan

Reputation: 8277

Just use a lambda function like this:

for (int i = 1; i < numButtons; i++)
{
    QPushButton *btn = new QPushButton(...);
    connect(btn, &QPushButton::clicked, [=]() {
        // Do something with 'i'
    }
}

Upvotes: 4

Related Questions