Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] Calling Slots with value/parameter?

    General and Desktop
    3
    9
    3172
    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.
    • M
      MrNoway last edited by

      so hi again,

      I need to call a SLOT with a parameter/value.
      how to do this, if i set up like this, the slot is not called

      @for (int i=0; i< value_x; i++){

         connect (push_button_here[i], SIGNAL(clicked()), this, SLOT(do_this(i)));   
      

      } @

      how can this be fixed?
      just tried solving this like forever.

      thank you already

      1 Reply Last reply Reply Quote 0
      • J
        Jake007 last edited by

        Hi!

        "QSignalMapper":http://doc.qt.digia.com/qt/qsignalmapper.html should do the trick.

        Regards,
        Jake


        Code is poetry

        1 Reply Last reply Reply Quote 0
        • M
          MrNoway last edited by

          thanks but you could provide little bit of code,

          i dont really understand how to get this run

          where should i write my own "do_this(i)" function?

          that example doesnt really help me

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            Something like this should work, assuming you have a slot do_this(int) in your code.

            @
            QSignalMapper *signalMapper = new QSignalMapper(this);

            for (int i = 0; i < value_x; ++i) {
            connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(push_button_here[i], i);
            }

            connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
            @
            Brain to keyboard, YMMV

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply Reply Quote 0
            • J
              Jake007 last edited by

              I can't find a project in which I played with QSignalMapper, but based on docs, it should be something like this:

              @
              QSignalMapper *mapper = new QSignalMapper(this);

              for(int i=0; i<0; i<value_x; i++)
              {
              connect(push_button_here[i], SIGNAL(clicked()), mapper, SLOT(map()));
              mapper->setMapping(push_button_here[i], i);
              }

              connect(mapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));@

              Note: Not tested!


              Code is poetry

              1 Reply Last reply Reply Quote 0
              • M
                MrNoway last edited by

                [quote author="mlong" date="1354648588"]Something like this should work, assuming you have a slot do_this(int) in your code.

                @
                QSignalMapper *signalMapper = new QSignalMapper(this);

                for (int i = 0; i < value_x; ++i) {
                connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
                signalMapper->setMapping(push_button_here[i], i);
                }

                connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
                @
                Brain to keyboard, YMMV[/quote]

                WOW Thats amazing!

                great working!

                thanks a lot

                1 Reply Last reply Reply Quote 0
                • J
                  Jake007 last edited by

                  Don't forget to mark as solved.

                  And mlong, you were faster :) . I had to read all the docs first.

                  Regards,
                  Jake


                  Code is poetry

                  1 Reply Last reply Reply Quote 0
                  • M
                    mlong last edited by

                    Glad it's working :)

                    And Jake007, I like how our code is very much the same, though. Almost as if we both were modifying the example code.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply Reply Quote 0
                    • J
                      Jake007 last edited by

                      Well... I did modify the example code :) .

                      Oh and one more solution I forgot to mention.

                      You could use sender() method ( in do_this(), without parameters), which returns QObject *. You can then cast it to QPushButton * or whatever widget you have. But I wouldn't recommend using it, as there might be problems if you call method directly or is accidentally called with signal from object with different type.
                      But might come in handy in a few lazy cases ;) .


                      Code is poetry

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post