Home > database >  connect dynamically created buttons in qt5
connect dynamically created buttons in qt5

Time:09-26

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.

CodePudding user response:

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'
    }
}
  • Related