PDA

View Full Version : Working with Slots



Ali Reza
27th October 2012, 12:56
hi all
suppose that i have two QPushButton that named A , B .
i want connect clicked signal of button A,B to a slot that take a parameter(int id).
for example: when i clicked button A, my slot be called with '1' for id parameter and about button B with '2'.
i do not know how to implement this.
i used the following code but gives me some errors.


/* in my class */
private slots:
void myslot(int id);

// *********************
/* for use this slot i use the below code */
connect(btn_A , SIGNAL(clicked()) , this , SLOT(myslot(1));
connect(btn_B , SIGNAL(clicked()) , this , SLOT(myslot(2));

alizadeh91
27th October 2012, 13:24
salam!
you can't do this. the order of signal parameters have to be same as slot parameters. in other words u can't have a slot that takes one argument and connect it to a signal without any args.

In addition your implementation of signal and slot is incorrect. it's better to have a study about this. In the slot's argument you have to put type of argument not its value! the value is sent by signal

Zlatomir
27th October 2012, 14:51
As alizadeh91 already said you should learn the basics about signals and slots from documentation, see this link (http://doc.qt.digia.com/qt/signalsandslots.html).

And for your problem a solution might be to use QSignalMapper (http://doc.qt.digia.com/qt/qsignalmapper.html) //but if you only have two buttons you might want to create separate slots connected to click signal and from within those slots call myslot with the corresponding parameter.

Ali Reza
28th October 2012, 13:17
thanks a lot a lot ... Mr Zlatomir
my problem solved by using QSignalMapper
thanks again...