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. Supply my own parameter (different from the Signal) to the Slot
Qt 6.11 is out! See what's new in the release blog

Supply my own parameter (different from the Signal) to the Slot

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 10.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.
  • A Offline
    A Offline
    Adelost
    wrote on last edited by
    #1

    I would like to supply my own parameter (different from the Signal) to the Slot. I know this code doesn't work, but perhaps it illustrates the idea of what I am trying to accomplish.

    @QAction* a;
    a = new QAction(this);
    connect(action, SIGNAL(triggered()), this, SLOT(setFoo(1.0f)));
    a = new QAction(this);
    connect(action, SIGNAL(triggered()), this, SLOT(setFoo(2.0f)));@

    Is this possible, or what would you have done when faced with similar problem?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      akonradwesolutions
      wrote on last edited by
      #2

      QSignalMapper could be the solution to your problem:

      @
      QAction* a1 = new QAction(this);
      QAction* a2 = new QAction(this);

      QSignalMapper* mapper = new QSignalMapper(this);

      mapper->setMapping(a1, 1);
      mapper->setMapping(a2, 2);

      connect(a1, SIGNAL(triggered()), mapper, SLOT(map()));
      connect(a2, SIGNAL(triggered()), mapper, SLOT(map()));

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

      QSignalMapper allows you to map QObject instances to integer, string and qobject pointers.
      An explicit mapping to float or double is not possible, but that can be worked around.

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        [quote author="Adelost" date="1367049667"]I would like to supply my own parameter (different from the Signal) to the Slot.[/quote]

        this is only possible in Qt5 and with a C++11 compatible compiler (also you need to have C++11 support configured Qt). See "here":http://qt-project.org/wiki/New_Signal_Slot_Syntax.
        Keyword: "c++11 lambda expressions"

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • A Offline
          A Offline
          akonradwesolutions
          wrote on last edited by
          #4

          I assumed the use of Qt 4 from the connection syntax in your code passage. In Qt5 with C++11 you can use the mentioned lambda expressions as well as std::bind:

          @
          #include <functional>
          // ...

          // std::bind
          std::function<void()> f1 = std::bind(&MyClass::mySlot, this, 5.0);
          connect(this, &MyClass::mySignal, f1);

          // lambda expression
          std::function<void()> f2 = this { mySlot(5.0); };
          connect(this, &MyClass::mySignal, f2);
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Adelost
            wrote on last edited by
            #5

            Thanks! The QSignalMapper worked quite well for my usage.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              Either that, or just create a simple wrapper with another slot inside of which you emit the signal with the desired parameter. I guess QSM does the same thing internally. The C++11 lambda solution is the same concept, only instead of a named slot an unnamed function is used.

              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