Unsolved Qt slot with default arguments not working
-
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?
-
@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();});
-
@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); }
-
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.