Create dynamic buttons with function
Solved
General and Desktop
-
Hi,
I have to dynamiclly create buttons in horizontalLayout and connect this buttons to my function.
I create button in this way:
for(auto & mTitle : pattern.mTitles) { ui->horizontalLayout->addWidget(new QPushButton(mTitle)); }
How can I connect the function to this buttons?
-
@ppitu said in Create dynamic buttons with function:
How can I connect the function to this buttons?
I don't really understand your problem here.
Use the instance and connect the right signal to the wanted function.For example, with a lambda function, I would do it like this:
for(auto & mTitle : pattern.mTitles) { auto btn = new QPushButton (mTitle); connect (btn, & QPushButton :: clicked, this, [btn] () { qDebug() << "Button" << btn << "clicked"; }); ui->horizontalLayout->addWidget(btn ); }
-
@ppitu said in Create dynamic buttons with function:
I first time do it in Qt and i cant find tutorial.
Then you don't search very well, for example in the
QObject:connect()
documentation:
https://doc.qt.io/qt-5/qobject.html#connect