PDA

View Full Version : Using QSIgnalMapper with multiple pushbuttons



akshaysulakhe
1st August 2013, 13:46
Hello friends, I am using multiple QPushButtons in my project. All of them should go inside 1 slot. So i am using QSignalMapper. My ui object is UI. And the pushbuttons are already created in designer. Lets say they are named pushbutton1, pushbutton2, etc. Now i want to map all of them in a signal mapper. So one would ideally use a for loop
Syntax :
for(i=0;i<10;i++){
connect(UI.pushbutton[i],SIGNAL(clicked()),signalmapper,SLOT(map()));
signalMapper->setMapping(UI.pushButton[i],i);
}

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.

Santosh Reddy
1st August 2013, 14:18
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;
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);
}
}

akshaysulakhe
1st August 2013, 15:21
Hello, Thank you for the reply. For now, i wrote it manually, but it still wont work. Syntax:
connect(ui.PushButton1, SIGNAL(clicked()),signalMapper,SLOT(map()))
Did similar for 10 buttons. I cant find child for all buttons, as there are other buttons also in the parent.
then i did
connect(signalMapper,SIGNAL(mapped(const QString &)),this, SIGNAL(clicked(const QString &)));
connect(signalMapper, SIGNAL(mapped(const QString &)),SLOT(changeImage(const QString &)));

This compiles, but when i click the button, i should get the button name in ChangeImage, which i am not getting. Kindly let me know what to do.

Santosh Reddy
1st August 2013, 15:30
You are missing this
signalMapper->setMapping(ui.pushButton1, "pushButton1");

akshaysulakhe
1st August 2013, 15:40
Good god man, Thanks, what a mistake, was struggling. Just one last query. As i have done the mapping without a for loop, it doesnt look very neat. And i cant use the code mentioned above, as there are other pushbuttons too. But all these 10 pushbuttons who have a single parent object name = QVerticalLayout and type QVBoxLayout...How can i modify the above code for that...Thanks again. :-)

Santosh Reddy
1st August 2013, 15:51
If you have the layout handle, then this way..


QSignalMapper * signalMapper;
QVBoxLayout * layout;

for(int i = 0; i < layout->count(); i++)
{
QLayoutItem * item = layout->itemAt(i);

QWidget * widget = item->widget();

if(widget)
{
QPushButton * pushButton = dynamic_cast<QPushButton *>(widget);

if(pushButton)
{
signalMapper->connect(pushButton, SIGNAL(clicked()), SLOT(map()));
signalMapper->setMapping(pushButton, pushButton->objectName());
}
}
}

anda_skoa
2nd August 2013, 17:45
Alternatively you could try using QButtonGroup, which is a kind of specialized signal mapper for buttons.

For your case it has the advantage of being available in designer (select multiple buttons -> right click -> assign to button group)

Cheers,
_