[SOLVED]QPushButton Signal connection
-
Hi all,
Is there is any method to connect the all QPushbuttons used in the project to same slot. instead of writing connect statement for individual QpushButtons?
Instead of,
@ connect(ui->continuousBut,SIGNAL(pressed()),this,SLOT(Beep()));
connect(ui->continuousBut,SIGNAL(released()),this,SLOT(Mute()));connect(ui->GateBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->GateBut,SIGNAL(released()),this,SLOT(Mute())); connect(ui->singleBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->singleBut,SIGNAL(released()),this,SLOT(Mute())); connect(ui->burstBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->burstBut,SIGNAL(released()),this,SLOT(Mute())); connect(ui->MANBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->MANBut,SIGNAL(released()),this,SLOT(Mute())); connect(ui->INTBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->INTBut,SIGNAL(released()),this,SLOT(Mute())); connect(ui->EXTBut,SIGNAL(pressed()),this,SLOT(Beep())); connect(ui->EXTBut,SIGNAL(released()),this,SLOT(Mute()));@
something like,
@connect(ui->QPushButton,SIGNAL(pressed()),this,SLOT(Beep()));
connect(ui->QPushButton,SIGNAL(released()),this,SLOT(Mute()));@ -
there is an "auto connect feature":http://qt-project.org/doc/qt-4.8/qmetaobject.html#connectSlotsByName in Qt, which connects objects with a slot automatically.
But i requires to set the "object name":http://qt-project.org/doc/qt-4.8/qobject.html#objectName-prop. But i don't know if this is possible when using ui-Files (never used them yet)... If it's possible to set a different QObject-objectname than the object name in the ui file to identify the object then you can try using this method. -
... or create "QButtonGroup":http://qt-project.org/doc/qt-5.0/qtwidgets/qbuttongroup.html, add all buttons in it and connect one of the QButtonGroup::buttonClicked signal to your slot
-
Hi,
There's also findChildren and you loop over the button list to connect them. That is unless you have other push buttons on the same UI. In this case you should give a name to these buttons to use findChildren
-
thanks friends.
QButtonGroup is what i want.thank you.
-
You can do this aslo:
@void ButtonGroup1Slot()
{
if(sender()->objectName().contains("Button1"))
{
//do this and so on for other buttons
}
//perform similar operations here
}@