Many PushButton



  • Hello, how can i to make this easier and shorter?
    I have many PushButton (more then 100) and doing similar thing.

    public slots:
    @
    void MyForm::on_pb_1_clicked() { Methode(pb_1, Array[0]);}
    void MyForm::on_pb_2_clicked() { Methode(pb_2, Array[1]);}
    .
    .
    .
    void MyForm::on_pb_n_clicked() { Methode(pb_n, Array[n-1]);}
    @

    pb_1, .... pb_n is many pushbutton.

    Edit: please use code tags, Andre



  • QSignalMapper!



  • Example:
    @
    QSignalMapper *btnGroup;
    btnGroup = new QSignalMapper(this);
    connect(btnGroup, SIGNAL(mapped(int)), this, SIGNAL(btnOutClickSlot(int)));

    void myclass::btnOutClickSlot(int btn)
    {
    //code here
    }

    QPushButton *tmp[2] = {pushButton,pushButton_2};
    for (quint16 i = 0; i < 2; i++)
    {
    btnGroup->setMapping(pushB[i], i);
    connect(tmp[i], SIGNAL(clicked()), btnGroup, SLOT(map()));
    }
    @



  • You can use sender() inside slots to find out who sent the signal.
    @
    void MyForm::on_pb_any_clicked()
    {
    if(sender() == pb_1)
    Methode(pb_1, Array[0]);
    if(sender() == pb_2)
    Methode(pb_2, Array[0]);
    .....
    ......
    }
    @



  • I highly recommend using QSignalMapper instead of using sender().



  • QSignalMapper is a perfect solution. TY!



  • It's a perfect solution except figuring out how to use it.
    Ron


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.