c++ - [SOLVED]Qt - I have a QPushButton QVector, which one was pressed? -


i have qlineedits , qcheckboxes qvectors - have qpushbutton qvector when element pressed, corresponding qlineedit , qcheckbox removed.

how can find out button pressed determine index remove? using checkboxes right easy little bulky liking.

thanks!

the straightforward way use qobject::sender() in button press slot find out button emitted signal. should iterate on vector of buttons:

qobject* obj = sender(); for(int i=0;i<buttonvector.count();i++) {     if( obj == qobject_cast<qobject *>(buttonvector[i]))     {        ...     } } 

one workaround use qobject::setobjectname , set names buttons add :

button.setobjectname(qstring("%1").arg(i)); 

and in slot can retrieve button number using object name :

void mainwindow::buttonclicked() {     qpushbutton *button = qobject_cast<qpushbutton *>(qobject::sender());      int number = button->objectname().toint(); } 

another way use qsignalmapper class collects set of parameterless signals, , re-emits them integer, string or widget parameters corresponding object sent signal. can have 1 like:

qsignalmapper * mapper = new qsignalmapper(this); qobject::connect(mapper,signal(mapped(int)),this,slot(buttonclicked(int))); 

when newing buttons, can connect clicked() signal of button map() slot of qsignalmapper , add mapping using setmapping when clicked() signaled button, signal mapped(int) emitted:

button = new qpushbutton();  qobject::connect(button, signal(clicked()),mapper,slot(map())); mapper->setmapping(button, i); 

this way whenever click button, mapped(int) signal of mapper emitted containing button number , consequently buttonclicked called parameter containing button number.


Comments