signalMapper is my object. With this code i can send the values to the slot and depending on that value i can take decisions. But it fails, compiler says, it doesnt know pushButton. But when i put pushButton1 or 2, it works. How to get that i in a for loop part working? Kindly let me know. Thank you for your time.
You cannot access pushButton in the loop using array operator.
You have two options.
1. use pushButton1, pushButton2.... etc instead of loop.
2. If you want to use lopp then get the list pushButtons from ui an then connect them, somthing like below. But be warned that this will connect the slot to all the pushButtons on the widget.
Ui::Form * ui;
...
ui->setupUi(this);
QObjectList childs = this->children();
for(int i = 0; i < childs.size(); i++)
{
QPushButton * pushButton
= qobject_cast<QPushButton
*>
(childs
[i
]);
if(pushButton)
{
this->connect(pushButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(pushButton, i);
}
}
Ui::Form * ui;
QSignalMapper * signalMapper;
...
ui->setupUi(this);
QObjectList childs = this->children();
for(int i = 0; i < childs.size(); i++)
{
QPushButton * pushButton = qobject_cast<QPushButton *>(childs[i]);
if(pushButton)
{
this->connect(pushButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(pushButton, i);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks