Connect clicked() signal of PushButton to a slot void(int)



  • Hi,
    I need to do something like this:
    @
    QObject::connect(CardMat[i].ibutton, SIGNAL(clicked()), this, SLOT(Modifica(i)));
    @

    i is an index in a for loop, i want each button in CardMat[] call slot "Modifica(int)" whit a different integer parameter (index i)
    How can i do this?



  • [[Doc:QSignalMapper]] comes into mind.



  • i don't understand what i have to do sorry



  • In the worst case you can define your own signals and slots.

    In your widget's class, define a slot like that:

    @
    public slots:
    void getButtonClicked();
    @

    and in the implementation call that function

    @
    void widget::getButtonClicked()
    {
    Modifica(i);
    }
    @

    and do the connection to that slot.



  • [quote author="WellSaid" date="1329316504"]i don't understand what i have to do sorry[/quote]

    The docs have an example. You will just need to replace the QStrings they use with ints. The respective methods are there.

    Otherwise, have a look at the "tag search":/search/tag/qsignalmapper please, there have been posted some examples previously.



  • I try:
    @
    connect(CardMat[i].ibutton, SIGNAL(clicked()), this, SLOT(map()));
    signalMapper -> setMapping(CardMat[i].ibutton, i);
    connect(signalMapper, SIGNAL(mapped(int)), this, SIGNAL(clicked(int)));
    @

    but now? where i have to write the code of my slot?

    EDIT: This code give some errors

    EDIT 2: I solved in this way:

    1. In constructor set the ObjectName of each button, like this:
      @
      for(int i = 0; i < 16; i++){
      CardMat[i].ibutton = new QPushButton(this);
      char name[7];
      sprintf(name, "%d_card", i);
      CardMat[i].ibutton -> setObjectName(QString::fromAscii(name));
      ...
      @
      So now the first char of the name of each button is the number in the matrix CardMat[].

    Then in the "Modfica()" slot, to obtain the number of the sender object, i do this:
    @
    void PlayForm::Modifica(){
    //Ottengo il numero della carta che ha chiamato Modifica()
    char* str = sender()->objectName().toAscii().data();
    int i = 0;
    for(int c = 0; c < strlen(str) && str[c] != '_'; c++){
    i += (str[c] -'0');
    i *= 10;
    }
    i /= 10;
    //--------------------------------------------------------

    }
    @



  • You are supposed to adapt the code, not copy it char to char and do a replace "QString" with "int"....

    You should connect the mapped() signal to a slot of your class, not (probably non-existent) signal (Hint add an argument to your Modifica slot!). You get the number of the clicked button in the slot for free.



  • Hi, There is a small change in the above mentioned problem. I am writing the connect signal and slot in main.cpp file.

    What are the slots that i can use from here. Can I incude a new class object and call a slot of that class



  • Hi, There is a small change in the above mentioned problem. I am writing the connect signal and slot in main.cpp file.

    What are the slots that i can use from here. Can I incude a new class object and call a slot of that class



  • I think a QButtonGroup would work well for what you want to do. Add all your buttons to a button group and connect the button group's buttonClicked(int id) signal to your slot. You need to include unique id (integer) when adding each of your buttons to the group.



  • I think a QButtonGroup would work well for what you want to do. Add all your buttons to a button group and connect the button group's buttonClicked(int id) signal to your slot. You need to include unique id (integer) when adding each of your buttons to the group.


Log in to reply
 

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