Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

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

    General and Desktop
    5
    11
    19764
    Loading More Posts
    • 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
      WellSaid last edited by

      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 Reply Quote 0
      • G
        goetz last edited by

        [[Doc:QSignalMapper]] comes into mind.

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

        1 Reply Last reply Reply Quote 0
        • W
          WellSaid last edited by

          i don't understand what i have to do sorry

          1 Reply Last reply Reply Quote 0
          • T
            TheDestroyer last edited by

            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 Reply Quote 0
            • G
              goetz last edited by

              [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 Reply Quote 0
              • W
                WellSaid last edited by

                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 Reply Quote 0
                • G
                  goetz last edited by

                  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 Reply Quote 0
                  • S
                    sunil.nair last edited by

                    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 Reply Quote 0
                    • S
                      sunil.nair last edited by

                      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 Reply Quote 0
                      • M
                        mchinand last edited by

                        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 Reply Quote 0
                        • M
                          mchinand last edited by

                          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 Reply Quote 0
                          • First post
                            Last post