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. Why does a functor slot must have exactly the same number of arguments as signal?
Forum Update on Monday, May 27th 2025

Why does a functor slot must have exactly the same number of arguments as signal?

Scheduled Pinned Locked Moved Solved General and Desktop
connectslotslotsconnect slot
5 Posts 3 Posters 1.0k 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.
  • X Offline
    X Offline
    Xeonacid
    wrote on 16 Dec 2020, 10:28 last edited by
    #1

    Hi,
    https://doc.qt.io/qt-5/qobject.html#connect-4 says;

    A function can be connected to a given signal if the signal has at least as many argument as the slot. A functor can be connected to a signal if they have exactly the same number of arguments.

    I'd like to know why. Is it just by design or a implementation issue? Any technical details?
    Thanks in advance.

    J 1 Reply Last reply 16 Dec 2020, 10:49
    0
    • J jsulm
      16 Dec 2020, 10:51

      @J-Hilk But you can connect a signal which has more parameters than the slot.
      Apparently same does not work with functors.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 16 Dec 2020, 10:59 last edited by
      #4

      @jsulm apparently an error in the documentation

      void functor(){
          qDebug() << Q_FUNC_INFO;
      }
      
      class Widget : public QWidget
      {
          Q_OBJECT
          QWidget *center2;
      public:
          explicit Widget(QWidget *parent = nullptr)
              : QWidget(parent), center2(new QWidget(this))
          {
              connect(this, &Widget::signalWithArgument, functor);
              auto t = new QTimer(this);
              connect(t, &QTimer::timeout, this,[=]()->void{emit signalWithArgument(1);});
              t->start(1000);
          }
      
      signals:
          void signalWithArgument(int);
      };
      
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          Widget w;
          w.show();
      
          return a.exec();
      }
      
      #include "main.moc"
      

      works perfectly fine


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      X 1 Reply Last reply 16 Dec 2020, 11:39
      3
      • X Xeonacid
        16 Dec 2020, 10:28

        Hi,
        https://doc.qt.io/qt-5/qobject.html#connect-4 says;

        A function can be connected to a given signal if the signal has at least as many argument as the slot. A functor can be connected to a signal if they have exactly the same number of arguments.

        I'd like to know why. Is it just by design or a implementation issue? Any technical details?
        Thanks in advance.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 16 Dec 2020, 10:49 last edited by
        #2

        @Xeonacid
        A problem of the c++ language, really

        You can't call a function that expects arguments, without arguments


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        J 1 Reply Last reply 16 Dec 2020, 10:51
        0
        • J J.Hilk
          16 Dec 2020, 10:49

          @Xeonacid
          A problem of the c++ language, really

          You can't call a function that expects arguments, without arguments

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Dec 2020, 10:51 last edited by
          #3

          @J-Hilk But you can connect a signal which has more parameters than the slot.
          Apparently same does not work with functors.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 16 Dec 2020, 10:59
          4
          • J jsulm
            16 Dec 2020, 10:51

            @J-Hilk But you can connect a signal which has more parameters than the slot.
            Apparently same does not work with functors.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 16 Dec 2020, 10:59 last edited by
            #4

            @jsulm apparently an error in the documentation

            void functor(){
                qDebug() << Q_FUNC_INFO;
            }
            
            class Widget : public QWidget
            {
                Q_OBJECT
                QWidget *center2;
            public:
                explicit Widget(QWidget *parent = nullptr)
                    : QWidget(parent), center2(new QWidget(this))
                {
                    connect(this, &Widget::signalWithArgument, functor);
                    auto t = new QTimer(this);
                    connect(t, &QTimer::timeout, this,[=]()->void{emit signalWithArgument(1);});
                    t->start(1000);
                }
            
            signals:
                void signalWithArgument(int);
            };
            
            
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                Widget w;
                w.show();
            
                return a.exec();
            }
            
            #include "main.moc"
            

            works perfectly fine


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            X 1 Reply Last reply 16 Dec 2020, 11:39
            3
            • J J.Hilk
              16 Dec 2020, 10:59

              @jsulm apparently an error in the documentation

              void functor(){
                  qDebug() << Q_FUNC_INFO;
              }
              
              class Widget : public QWidget
              {
                  Q_OBJECT
                  QWidget *center2;
              public:
                  explicit Widget(QWidget *parent = nullptr)
                      : QWidget(parent), center2(new QWidget(this))
                  {
                      connect(this, &Widget::signalWithArgument, functor);
                      auto t = new QTimer(this);
                      connect(t, &QTimer::timeout, this,[=]()->void{emit signalWithArgument(1);});
                      t->start(1000);
                  }
              
              signals:
                  void signalWithArgument(int);
              };
              
              
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  Widget w;
                  w.show();
              
                  return a.exec();
              }
              
              #include "main.moc"
              

              works perfectly fine

              X Offline
              X Offline
              Xeonacid
              wrote on 16 Dec 2020, 11:39 last edited by
              #5

              @J-Hilk Thanks! Hope it can be fixed soon.
              @jsulm Same thanks!

              1 Reply Last reply
              0

              1/5

              16 Dec 2020, 10:28

              • Login

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