[Solved] How to make multi QPushButton to activate slot
-
Hello.
I am trying to make a simple game that will have 9 QPushButtons. I am trying to make single conection that will have all 9 QpushButton and slot, and when I click the button, that button will change text, with help of slot.
I have tried doing this like:
@connect(button1 | button2 | button3 | button4 | button5 | button6 | button7 | button8 | button9 |, SIGNAL(clicked()), this, SLOT(buttonPressed()));@
But it dosent work.Do you have some idea how can I do this???
-
You need to have nine different connect statements, but you can connect those to the same slot.
-
If you need to differentiate between what button was clicked, you should also look into [[doc:QSignalMapper]]. Also note that you can use a loop to do the connects, if you don't want to make 9 connect statements in your code.
-
Thank you very much for your help.
I have made connections with QSignalMapper, but I still have one problem.
I am trying to make a function that will on button click change text on the clicked button.
So far I have made a function that changes text on button click but only on the last button, so when I click on the any button it will only change text on the last button.
Can you please help me solve this problem.
-
You could keep using QSignalMapper for that, using the mapping to QWidget, but perhaps that gets a bit tedious. You could also use something like this:
@
MyClass::buttonClicked() {
QAbstractButton* button = qobject_cast<QAbstractButton*>(sender());
if (!button)
return; // not called from buttonbutton->setText("Clicked");
}
@And then simply connect all the buttons clicked signals to this one slot.
-
Thank you very much for help