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. Signals and Slot using Functor
Forum Updated to NodeBB v4.3 + New Features

Signals and Slot using Functor

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 2.9k 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.
  • T Offline
    T Offline
    TopNotch_Mach07
    wrote on last edited by
    #1

    Hi ,

    Can anyone explain what is going on this code. How does the Functor class acts as a Slot ?

    Sorry for such a dumb question , Newbie to C++ and Qt.

    class Functor {
    public:
    Functor(Object *object, const QString &str) :
         m_object(object), m_str(str) {}
         void operator()(int x, int y) const {
         m_object->set(x, y, m_str);
    }
    private:
       Object *m_object;
        QString m_str;
    };
        connect(obj1, SIGNAL(coordChanged(int, int)),
        Functor("Some Text"));
    
    

    Thanks.

    m.sueM 1 Reply Last reply
    0
    • T TopNotch_Mach07

      Hi ,

      Can anyone explain what is going on this code. How does the Functor class acts as a Slot ?

      Sorry for such a dumb question , Newbie to C++ and Qt.

      class Functor {
      public:
      Functor(Object *object, const QString &str) :
           m_object(object), m_str(str) {}
           void operator()(int x, int y) const {
           m_object->set(x, y, m_str);
      }
      private:
         Object *m_object;
          QString m_str;
      };
          connect(obj1, SIGNAL(coordChanged(int, int)),
          Functor("Some Text"));
      
      

      Thanks.

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      @TopNotch_Mach07 said in Signals and Slot using Functor:

      How does the Functor class acts as a Slot ?

      Hi,
      it's because your connect statement connects to a function of signature (int x, int y) and the class has an operator() of the same signature.
      -Michael.

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        see https://wiki.qt.io/New_Signal_Slot_Syntax

        New: connecting to simple function
        The new syntax can even connect to functions, not just QObjects

        in this case the operator() of the object is treated as a function

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • Andy314A Offline
          Andy314A Offline
          Andy314
          wrote on last edited by Andy314
          #4

          Your Functor class is not a functor !
          The constructor should only set parameter for the functor function. You must define a functor function in your class.

          Class Functor
          {
          .......................
          Returntype operator()(Argumentlist) { .....; return ....; }
          }

          Functor f(ConstructorArguments);
          auto x=f(FunctorArguments); // here the syntax is like a normal function call

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            If you are new to to c++
            Might be good to know the what functor really means.
            http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html

            TLDR: Its basically just a function inside a class.
            We pass the class to other class where they expect a function pointer/adress but the magic of "operator ()"
            makes it work as otherwise it would complain about types.

            1 Reply Last reply
            1

            • Login

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