Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect clicked() signal of PushButton to a slot void(int)

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 21.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WellSaid
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      [[Doc:QSignalMapper]] comes into mind.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WellSaid
        wrote on last edited by
        #3

        i don't understand what i have to do sorry

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheDestroyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            [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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WellSaid
              wrote on last edited by
              #6

              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;
              //--------------------------------------------------------

              }
              @

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sunil.nair
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sunil.nair
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mchinand
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved