Utiliser une commande lambda avec connect
-
Bonjour,
j'essaye d'utiliser un QOject::connect pour modifier une variable de type int :QObject::connect(bouton, SIGNAL(clicked()), this, [this](){ variable++; });
Or, cela me sort plusieurs erreurs :
no matching function for call to ‘OptionMenuView::connect(QPushButton*&, const char*, vue*, vue::vue(QWidget*)::<lambda(int)>)’ | }); | ^
et
erreur : no type named ‘Object’ in ‘struct QtPrivate::FunctionPointer<OptionMenuView::OptionMenuView(QWidget*)::<lambda(int)> >’ In file included from /home/-/Qt/6.4.1/gcc_64/include/QtWidgets/qgraphicsitem.h:8, from /home/-/Qt/6.4.1/gcc_64/include/QtWidgets/QGraphicsItem:1, from /home/-/-/rpg/main/optionmenuview.h:4, from /home/-/-/rpg/main/optionmenuview.cpp:1: /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:181:36: note: candidate: ‘static QMetaObject::Connection QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)’ 181 | static QMetaObject::Connection connect(const QObject *sender, const char *signal, | ^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:182:62: note: no known conversion for argument 4 from ‘OptionMenuView::OptionMenuView(QWidget*)::<lambda(int)>’ to ‘const char*’ 182 | const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection); | ~~~~~~~~~~~~^~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:184:36: note: candidate: ‘static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)’ 184 | static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, | ^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:184:86: note: no known conversion for argument 2 from ‘const char*’ to ‘const QMetaMethod&’ 184 | static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, | ~~~~~~~~~~~~~~~~~~~^~~~~~ /home/sit/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:432:32: note: candidate: ‘QMetaObject::Connection QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const’ 432 | inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, | ^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:433:57: note: no known conversion for argument 3 from ‘OptionMenuView*’ to ‘const char*’ 433 | const char *amember, Qt::ConnectionType atype) const | ~~~~~~~~~~~~^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:201:43: note: candidate: ‘template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType)’ 201 | static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, | ^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:201:43: note: template argument deduction/substitution failed: /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h: In substitution of ‘template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = const char*; Func2 = OptionMenuView::OptionMenuView(QWidget*)::<lambda(int)>]’: /home/-/nicolas/rpg/main/optionmenuview.cpp:33:6: required from here /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:201:43: error: no type named ‘Object’ in ‘struct QtPrivate::FunctionPointer<OptionMenuView::OptionMenuView(QWidget*)::<lambda(int)> >’ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:233:13: note: candidate: ‘template<class Func1, class Func2> static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)’ 233 | connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot) | ^~~~~~~ /home/-/Qt/6.4.1/gcc_64/include/QtCore/qobject.h:233:13: note: template argument deduction/substitution failed: /home/-/-/-/main/optionmenuview.cpp:33:6: note: candidate expects 3 arguments, 4 provided | }); | ^
et encore 2 autres très similaires.
Si quelqu'un peut m'aider, je le remercie d'avance. -
-
-
@Futuray-Programming
On ne peut pas avoirSIGNAL
/SLOT()
macros avec leconnect()
moderne qui permet un lambda:QObject::connect(bouton, &QPushButton::clicked, this, [this](){
Ne utilisez plus le syntaxe ancien avec les macros.
N'utilisez plus l'ancienne syntaxe avec les macros. -
@Futuray-Programming said in Utiliser une commande lambda avec connect:
QObject::connect(bouton, SIGNAL(clicked()), this, this{
variable++;
});Essaye avec la nouvelle syntaxe:
connect(bouton, &QPushButton::clicked, this, [this](){ variable++; });
-
@Futuray-Programming
On ne peut pas avoirSIGNAL
/SLOT()
macros avec leconnect()
moderne qui permet un lambda:QObject::connect(bouton, &QPushButton::clicked, this, [this](){
Ne utilisez plus le syntaxe ancien avec les macros.
N'utilisez plus l'ancienne syntaxe avec les macros. -
Ah oui, en effet, je ne connaissais pas la nouvelle syntaxe.
Je vais mètre à jour tout le code du coup.
👍 -
@Futuray-Programming said in Utiliser une commande lambda avec connect:
Je vais mètre à jour tout le code du coup.
Combien de mètres de code ? :)
-
-
Nouvelle syntaxe qui a 10 ans pour info ;)