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. [SOLVED] Calling Slots with value/parameter?

[SOLVED] Calling Slots with value/parameter?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.5k Views
  • 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 Offline
    M Offline
    MrNoway
    wrote on last edited by
    #1

    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
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Hi!

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

      Regards,
      Jake


      Code is poetry

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrNoway
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          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
          0
          • J Offline
            J Offline
            Jake007
            wrote on last edited by
            #5

            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
            0
            • M Offline
              M Offline
              MrNoway
              wrote on last edited by
              #6

              [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
              0
              • J Offline
                J Offline
                Jake007
                wrote on last edited by
                #7

                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
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  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
                  0
                  • J Offline
                    J Offline
                    Jake007
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

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