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 16.7k 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.
  • M Mark81
    22 Jan 2019, 20:16

    @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'

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 22 Jan 2019, 20:17 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 22 Jan 2019, 20:25 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 22 Jan 2019, 20:33 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 22 Jan 2019, 20:36
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Jan 2019, 20:36 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 22 Jan 2019, 20:39
          1
          • M mrjj
            22 Jan 2019, 20:33

            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 22 Jan 2019, 20:36 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!

            V 1 Reply Last reply 23 Jan 2019, 11:01
            0
            • S SGaist
              22 Jan 2019, 20:36

              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 22 Jan 2019, 20:39 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 22 Jan 2019, 22:03 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 23 Jan 2019, 08:22
                0
                • S SGaist
                  22 Jan 2019, 22:03

                  Out of curiosity, what errors are you getting ?

                  M Offline
                  M Offline
                  Mark81
                  wrote on 23 Jan 2019, 08:22 last edited by
                  #15

                  @SGaist said in QTimer with lambda functions:

                  Out of curiosity, what errors are you getting ?

                  0_1548231709712_Clipboard 1.jpg

                  J 1 Reply Last reply 23 Jan 2019, 08:55
                  0
                  • M Mark81
                    23 Jan 2019, 08:22

                    @SGaist said in QTimer with lambda functions:

                    Out of curiosity, what errors are you getting ?

                    0_1548231709712_Clipboard 1.jpg

                    J Offline
                    J Offline
                    JonB
                    wrote on 23 Jan 2019, 08:55 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
                      22 Jan 2019, 20:36

                      @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!

                      V Offline
                      V Offline
                      VRonin
                      wrote on 23 Jan 2019, 11:01 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 23 Jan 2019, 12:43
                      3
                      • V VRonin
                        23 Jan 2019, 11:01

                        @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 23 Jan 2019, 12:43 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

                        17/18

                        23 Jan 2019, 11:01

                        • Login

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