QTimer with lambda functions
-
@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' -
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. -
On side note, I would rather using something like:
QMetaObject::invokeMethod(this, "validationFailed", Qt::QueuedConnection, Q_ARG(QModelIndex, idx));
-
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.@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! -
On side note, I would rather using something like:
QMetaObject::invokeMethod(this, "validationFailed", Qt::QueuedConnection, Q_ARG(QModelIndex, idx));
@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. -
Out of curiosity, what errors are you getting ?
-
@Mark81
I'm not a C++er, but since the only argument of typeQObject*
you are passing is yourthis
as the first parameter, I assume it must be that yourthis
is currentlyconst
and causes the error. You must be inside a function which is itself declaredconst
, and hencethis
isconst
and unacceptable toQMetaObject::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!
-
@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!@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
isconst
insidesetModelData
. Not something easy to see. Exactly the same reason whyQMetaObject::invokeMethod
doesn't work.I'm surprised the lambda version works without declaring the signal as
const
-
@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
isconst
insidesetModelData
. Not something easy to see. Exactly the same reason whyQMetaObject::invokeMethod
doesn't work.I'm surprised the lambda version works without declaring the signal as
const