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. Qt slot with default arguments not working
Forum Updated to NodeBB v4.3 + New Features

Qt slot with default arguments not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 2.3k Views 3 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.
  • J Offline
    J Offline
    JuhaSim
    wrote on last edited by
    #1

    Having slot

    private slots:
    void slot(bool param = true);
    

    And connecting

    QTimer::singleShot(6000, this, &Class::slot);
    

    Creates a compilation error:

    qglobal.h:121:49: error: static assertion failed: The slot requires more arguments than the signal provides.
    qobject.h:239:9: note: in expansion of macro ‘Q_STATIC_ASSERT_X’
    

    Qt 5.12.1

    https://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots-with-default-arguments
    The documentation says that: The signatures of signals and slots may contain arguments, and the arguments can have default values.

    Is this a bug?

    J.HilkJ Pablo J. RoginaP 2 Replies Last reply
    0
    • J JuhaSim

      Having slot

      private slots:
      void slot(bool param = true);
      

      And connecting

      QTimer::singleShot(6000, this, &Class::slot);
      

      Creates a compilation error:

      qglobal.h:121:49: error: static assertion failed: The slot requires more arguments than the signal provides.
      qobject.h:239:9: note: in expansion of macro ‘Q_STATIC_ASSERT_X’
      

      Qt 5.12.1

      https://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots-with-default-arguments
      The documentation says that: The signatures of signals and slots may contain arguments, and the arguments can have default values.

      Is this a bug?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by aha_1980
      #2

      @JuhaSim said in Qt slot with default arguments not working:

      Is this a bug?

      no, default arguments for slots is a feature for Qt4 Syntax only, the Qt5 one does not support it, sadly enough.

      You have two options:

      QTimer::singleShot(6000, this, SLOT(slot());
      

      This may be wrong, been a while since I used Qt4 with default argument!

      or a lambda

      QTimer::singleShot(6000, this, [=]()->void{slot();});
      

      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.

      1 Reply Last reply
      9
      • J JuhaSim

        Having slot

        private slots:
        void slot(bool param = true);
        

        And connecting

        QTimer::singleShot(6000, this, &Class::slot);
        

        Creates a compilation error:

        qglobal.h:121:49: error: static assertion failed: The slot requires more arguments than the signal provides.
        qobject.h:239:9: note: in expansion of macro ‘Q_STATIC_ASSERT_X’
        

        Qt 5.12.1

        https://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots-with-default-arguments
        The documentation says that: The signatures of signals and slots may contain arguments, and the arguments can have default values.

        Is this a bug?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @JuhaSim said in Qt slot with default arguments not working:

        void slot(bool param = true);

        Setting Qt 4.x vs 5.x syntax for connect() aside, for me the question is when the called slot will receive a false value? what will make the argument have a different value from the default one?

        Assuming conditionX I can think of an interim slot called by the timer off which in turns calls the current slot. Pseudo-code:

        private slots:
        void slotTemp();
        
        private:
        void slot(bool param = true);
        
        ....
        
        QTimer::singleShot(6000, this, &Class::slotTemp);
        
        void slotTemp() {
            slot(conditionX);
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          AFAIK, you can't have fewer signal parameters using the syntax (see here).

          However, you can do what you want using a lambda or use the old syntax.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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