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. QTimer with lambda functions
Forum Updated to NodeBB v4.3 + New Features

QTimer with lambda functions

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 17.0k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by SGaist
    #2

    Hi,

    Remove the &.
    You should also add the emit keyword, that will make things clearer.

    And you have to pass this explicitly

    [edit: Added missing this handling SGaist]

    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
    • M Offline
      M Offline
      Mark81
      wrote on last edited by
      #3

      Like this?

      QTimer::singleShot(10, this, [idx] () { emit CustomDelegateOperators::validationFailed(idx); });
      

      it quite the same error:

      'this' cannot be implicitly captured in this context
      cannot call member function 'void CustomDelegateOperators::validationFailed(QModelIndex) const' without object
      no matching function for call to 'singleShot'

      I cannot fully understand what these messages really mean.

      mrjjM 1 Reply Last reply
      0
      • M Mark81

        Like this?

        QTimer::singleShot(10, this, [idx] () { emit CustomDelegateOperators::validationFailed(idx); });
        

        it quite the same error:

        'this' cannot be implicitly captured in this context
        cannot call member function 'void CustomDelegateOperators::validationFailed(QModelIndex) const' without object
        no matching function for call to 'singleShot'

        I cannot fully understand what these messages really mean.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Mark81
        Hi
        What about

         QTimer::singleShot(10, this, [this, idx] () {
                emit validationFailed(idx);
            });
        

        Compiles here. (using Mainwindow as slot holder)

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mark81
          wrote on last edited by
          #5

          It doesn't compile here:

          customdelegateoperators.cpp: In member function 'virtual void CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const':
          customdelegateoperators.cpp:70:17: error: no matching function for call to 'QTimer::singleShot(int, const CustomDelegateOperators*, CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()>)'
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:83:17: note: candidate: static void QTimer::singleShot(int, const QObject*, const char*)
               static void singleShot(int msec, const QObject *receiver, const char *member);
                           ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:83:17: note:   no known conversion for argument 3 from 'CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()>' to 'const char*'
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:84:17: note: candidate: static void QTimer::singleShot(int, Qt::TimerType, const QObject*, const char*)
               static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
                           ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:84:17: note:   candidate expects 4 arguments, 3 provided
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:102:24: note: candidate: template<class Duration, class Func1> static void QTimer::singleShot(Duration, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func1)
               static inline void singleShot(Duration interval, const typename QtPrivate::FunctionPointer<Func1>::Object *receiver, Func1 slot)
                                  ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:102:24: note:   template argument deduction/substitution failed:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h: In substitution of 'template<class Duration, class Func1> static void QTimer::singleShot(Duration, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func1) [with Duration = int; Func1 = CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()>]':
          customdelegateoperators.cpp:70:17:   required from here
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:102:24: error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()> >'
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:107:24: note: candidate: template<class Duration, class Func1> static void QTimer::singleShot(Duration, Qt::TimerType, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func1)
               static inline void singleShot(Duration interval, Qt::TimerType timerType, const typename QtPrivate::FunctionPointer<Func1>::Object *receiver,
                                  ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:107:24: note:   template argument deduction/substitution failed:
          customdelegateoperators.cpp:70:17: note:   cannot convert '(const CustomDelegateOperators*)this' (type 'const CustomDelegateOperators*') to type 'Qt::TimerType'
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:123:13: note: candidate: template<class Duration, class Func1> static typename std::enable_if<((! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction) && (! std::is_same<const char*, Func1>::value)), void>::type QTimer::singleShot(Duration, Func1)
                       singleShot(Duration interval, Func1 slot)
                       ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:123:13: note:   template argument deduction/substitution failed:
          customdelegateoperators.cpp:70:17: note:   candidate expects 2 arguments, 3 provided
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:130:13: note: candidate: template<class Duration, class Func1> static typename std::enable_if<((! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction) && (! std::is_same<const char*, Func1>::value)), void>::type QTimer::singleShot(Duration, Qt::TimerType, Func1)
                       singleShot(Duration interval, Qt::TimerType timerType, Func1 slot)
                       ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:130:13: note:   template argument deduction/substitution failed:
          customdelegateoperators.cpp:70:17: note:   cannot convert '(const CustomDelegateOperators*)this' (type 'const CustomDelegateOperators*') to type 'Qt::TimerType'
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:138:13: note: candidate: template<class Duration, class Func1> static typename std::enable_if<((! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction) && (! std::is_same<const char*, Func1>::value)), void>::type QTimer::singleShot(Duration, QObject*, Func1)
                       singleShot(Duration interval, QObject *context, Func1 slot)
                       ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:138:13: note:   template argument deduction/substitution failed:
          customdelegateoperators.cpp:70:17: note:   cannot convert '(const CustomDelegateOperators*)this' (type 'const CustomDelegateOperators*') to type 'QObject*'
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:145:13: note: candidate: template<class Duration, class Func1> static typename std::enable_if<((! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction) && (! std::is_same<const char*, Func1>::value)), void>::type QTimer::singleShot(Duration, Qt::TimerType, QObject*, Func1)
                       singleShot(Duration interval, Qt::TimerType timerType, QObject *context, Func1 slot)
                       ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:145:13: note:   template argument deduction/substitution failed:
          customdelegateoperators.cpp:70:17: note:   cannot convert '(const CustomDelegateOperators*)this' (type 'const CustomDelegateOperators*') to type 'Qt::TimerType'
                          });
                           ^
          In file included from C:\Qt\5.11.1\mingw53_32\include\QtCore/QTimer:1:0,
                           from customdelegateoperators.cpp:4:
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:183:17: note: candidate: static void QTimer::singleShot(std::chrono::milliseconds, const QObject*, const char*)
               static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
                           ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:183:17: note:   no known conversion for argument 1 from 'int' to 'std::chrono::milliseconds {aka std::chrono::duration<long long int, std::ratio<1ll, 1000ll> >}'
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:188:17: note: candidate: static void QTimer::singleShot(std::chrono::milliseconds, Qt::TimerType, const QObject*, const char*)
               static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
                           ^
          C:\Qt\5.11.1\mingw53_32\include\QtCore/qtimer.h:188:17: note:   candidate expects 4 arguments, 3 provided
          Makefile.Debug:10067: recipe for target 'debug/customdelegateoperators.o' failed
          

          By the way I set CONFIG += c++11 in the project file.

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

            Remove CustomDelegateOperators:: before the signal name.

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

            M 1 Reply Last reply
            1
            • SGaistS SGaist

              Remove CustomDelegateOperators:: before the signal name.

              M Offline
              M Offline
              Mark81
              wrote on last edited by
              #7

              @SGaist said in QTimer with lambda functions:

              Remove CustomDelegateOperators:: before the signal name.

              Unfortunately the error messages are still there:

              error: 'this' was not captured for this lambda function
              else QTimer::singleShot(10, this, [idx] () { emit validationFailed(idx); });
              ^
              error: cannot call member function 'void CustomDelegateOperators::validationFailed(QModelIndex) const' without object
              error: no matching function for call to 'QTimer::singleShot(int, const CustomDelegateOperators*, CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()>)'
              else QTimer::singleShot(10, this, [idx] () { emit validationFailed(idx); });
              ^
              error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()> >'
              error: no matching function for call to 'singleShot'

              mrjjM 1 Reply Last reply
              0
              • M Mark81

                @SGaist said in QTimer with lambda functions:

                Remove CustomDelegateOperators:: before the signal name.

                Unfortunately the error messages are still there:

                error: 'this' was not captured for this lambda function
                else QTimer::singleShot(10, this, [idx] () { emit validationFailed(idx); });
                ^
                error: cannot call member function 'void CustomDelegateOperators::validationFailed(QModelIndex) const' without object
                error: no matching function for call to 'QTimer::singleShot(int, const CustomDelegateOperators*, CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()>)'
                else QTimer::singleShot(10, this, [idx] () { emit validationFailed(idx); });
                ^
                error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<CustomDelegateOperators::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const::<lambda()> >'
                error: no matching function for call to 'singleShot'

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Mark81
                Hi
                I had to capture this in the [] for it to know the signal.

                QTimer::singleShot(10, this, [this, idx] () {
                        emit validationFailed(idx);
                    });
                

                notice the extra this in the [] besides idx

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mark81
                  wrote on last edited by
                  #9

                  I bet there is something "stupid"here like in the other thread :-)

                  0_1548188710856_Clipboard 1.jpg

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

                    Hi
                    Hmm, it does look correct.
                    Can you try without the first "this" (the context)

                        QTimer::singleShot(10,[this, idx] () {
                            emit validationFailed(idx);
                        });
                    
                    

                    and if that still complains, try a dummy to see if something with the surrounding code. like being in a switch.

                        QTimer::singleShot(10,[] () { });
                    
                    

                    if it complains about this too, its something else.

                    it seems to complain about CustomDelegateOperators cant be converted to a QObject which is odd as
                    QStyledItemDelegate is.

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

                      On side note, I would rather using something like:

                      QMetaObject::invokeMethod(this, "validationFailed", Qt::QueuedConnection, Q_ARG(QModelIndex, idx));
                      

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

                      M 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        Hi
                        Hmm, it does look correct.
                        Can you try without the first "this" (the context)

                            QTimer::singleShot(10,[this, idx] () {
                                emit validationFailed(idx);
                            });
                        
                        

                        and if that still complains, try a dummy to see if something with the surrounding code. like being in a switch.

                            QTimer::singleShot(10,[] () { });
                        
                        

                        if it complains about this too, its something else.

                        it seems to complain about CustomDelegateOperators cant be converted to a QObject which is odd as
                        QStyledItemDelegate is.

                        M Offline
                        M Offline
                        Mark81
                        wrote on last edited by
                        #12

                        @mrjj said in QTimer with lambda functions:

                        Can you try without the first "this" (the context)

                        HERE WE GO!!!!

                        This works!
                        But I'm honestly astonished about all these attempts! Please, don't misunderstand. I'm very grateful to you and all other guys, but I don't understand why there is so much confusion about the syntax!

                        VRoninV 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          On side note, I would rather using something like:

                          QMetaObject::invokeMethod(this, "validationFailed", Qt::QueuedConnection, Q_ARG(QModelIndex, idx));
                          
                          M Offline
                          M Offline
                          Mark81
                          wrote on last edited by
                          #13

                          @SGaist said in QTimer with lambda functions:

                          On side note, I would rather using something like:

                          QMetaObject::invokeMethod(this, "validationFailed", Qt::QueuedConnection, Q_ARG(QModelIndex, idx));
                          

                          Thanks but this doesn't compile LOL!
                          I'm going to stay with the other solution that apparently "fix" also the problem of the validation.

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

                            Out of curiosity, what errors are you getting ?

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

                            M 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Out of curiosity, what errors are you getting ?

                              M Offline
                              M Offline
                              Mark81
                              wrote on last edited by
                              #15

                              @SGaist said in QTimer with lambda functions:

                              Out of curiosity, what errors are you getting ?

                              0_1548231709712_Clipboard 1.jpg

                              JonBJ 1 Reply Last reply
                              0
                              • M Mark81

                                @SGaist said in QTimer with lambda functions:

                                Out of curiosity, what errors are you getting ?

                                0_1548231709712_Clipboard 1.jpg

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #16

                                @Mark81
                                I'm not a C++er, but since the only argument of type QObject* you are passing is your this as the first parameter, I assume it must be that your this is currently const and causes the error. You must be inside a function which is itself declared const, and hence this is const and unacceptable to QMetaObject::invokeMethod()?

                                You commented earlier that you found lambda syntax confusing, with which I whole-heartedly agree. They are a syntactic mess in many languages, especially C++ IMHO. I don't know, but you/others might like to peruse the little article https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f I came across; whoever it's written by, it lays things out in a nice, big font!

                                1 Reply Last reply
                                2
                                • M Mark81

                                  @mrjj said in QTimer with lambda functions:

                                  Can you try without the first "this" (the context)

                                  HERE WE GO!!!!

                                  This works!
                                  But I'm honestly astonished about all these attempts! Please, don't misunderstand. I'm very grateful to you and all other guys, but I don't understand why there is so much confusion about the syntax!

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #17

                                  @Mark81 said in QTimer with lambda functions:

                                  but I don't understand why there is so much confusion about the syntax!

                                  The problem is that this is const inside setModelData. Not something easy to see. Exactly the same reason why QMetaObject::invokeMethod doesn't work.

                                  I'm surprised the lambda version works without declaring the signal as const

                                  "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

                                  M 1 Reply Last reply
                                  3
                                  • VRoninV VRonin

                                    @Mark81 said in QTimer with lambda functions:

                                    but I don't understand why there is so much confusion about the syntax!

                                    The problem is that this is const inside setModelData. Not something easy to see. Exactly the same reason why QMetaObject::invokeMethod doesn't work.

                                    I'm surprised the lambda version works without declaring the signal as const

                                    M Offline
                                    M Offline
                                    Mark81
                                    wrote on last edited by
                                    #18

                                    @VRonin said in QTimer with lambda functions:

                                    I'm surprised the lambda version works without declaring the signal as const

                                    As you can see in the first post the signal is actually const:

                                    signals:
                                        void validationFailed(QModelIndex index) const;
                                    
                                    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