PDA

View Full Version : create multiple buttons with signal and slots



mohanakrishnan
14th November 2009, 07:44
hi
i want to show multiple pushbuttons in the form dynamically!!! that is the number of pushbuttons i am showing ,will vary at runtime
.So i want to connect each button with signal and slot.i have created a function(i.e slot)
which will be called when any of the button is clicked..


for(int i=0;i<count;i++)
{
QPushButton *labelbed =new QPushButton(roomgroup);
labelbed->setObjectName(bednumbersunderroom.at(i));

connect(labelbed,SIGNAL(clicked()),signalMapper,SL OT(map()));
signalMapper->setMapping(labelbed,labelbed); connect(signalMapper,SIGNAL(mapped(QWidget*)),this ,SLOT(onbedclicked(QWidget*)));
}
void Widget::onbedclicked(QWidget* label)
{


QMessageBox *msg=new QMessageBox(this);
msg->setText(label->objectName());
msg->show();
}


when any push button is clicked it displays message box but it displays as many messageboxes as push buttons...
for eg:if there are 3 push buttons,then if i click any one push button it displays 3 messagebox.But it should show only one messagebox..so how to do the signal slots
so that it will show one messagebox for each click!!!

thanks and regards
any help is highly appreciated
:confused::confused::confused:

Lykurg
14th November 2009, 08:19
connect(signalMapper,SIGNAL(mapped(QWidget*)),this ,SLOT(onbedclicked(QWidget*)));

must be outside your loop! because then you conntect the signal mapper n-times.

mohanakrishnan
14th November 2009, 08:23
hi
i ll check it and come back
thanks for ur help
:):)

hi Lykurg
it works well thanks for ur help
:D:D

caduel
14th November 2009, 08:46
place that statement outside of your loop:

connect(signalMapper,SIGNAL(mapped(QWidget*)),this ,SLOT(onbedclicked(QWidget*)));

spirit
14th November 2009, 09:42
Lykurg has already indicated that in 3 post ;)

caduel
14th November 2009, 10:14
Of course he has; when I replied that answer was not there for me to see. (Otherwise I would not have posted anymore.)
Happens now and then. Sorry (not my fault, though).

Lykurg
14th November 2009, 10:43
Lykurg has already indicated that in 3 post ;)

Wow, do you see my post 3times??? I'm here in the diaspora, in the middle of woods with a bad wlan...

squidge
14th November 2009, 12:00
[ Retracted ]

wysota
14th November 2009, 12:03
Guys, this is a thread that is a merge of two threads. Each of them replied in a different thread. Everything is ok (apart from the original author of the thread multi-threading).