no matching member function for call to 'connect'
-
Hello,
I am trying to use QSignalMapper to map many similar identifiable QLineEdit widgets in my program.
I am following instructions on this page: https://doc.qt.io/qt-5/qsignalmapper.html#details
However I get a compilation error (the title). I don't understand, QWidget inherits from QObject, so the member function exists, but I must be doing something wrong with the parameters. Help :)The line that produces this error is: (in the .cpp file)
connect(lineEdit, &QLineEdit::editingFinished, signalMapper, &QSignalMapper::map);
Here is the source code and header of the class I'm implementing:
lineedityarnsetpoint.h
#ifndef LINEEDITYARNSETPOINT_H #define LINEEDITYARNSETPOINT_H #include <QWidget> #include <QSignalMapper> #include <QLineEdit> #include <QGridLayout> //#include <QObject> class LineEditYarnSetpoint : public QWidget { Q_OBJECT public: LineEditYarnSetpoint(const int n, QWidget *parent = nullptr); signals: void editingFinished(const int feeder_index); private: QSignalMapper *signalMapper; }; #endif // LINEEDITYARNSETPOINT_H
lineedityarnsetpoint.cpp
#include "lineedityarnsetpoint.h" LineEditYarnSetpoint::LineEditYarnSetpoint(const int n, QWidget *parent) : QWidget(parent) { signalMapper = new QSignalMapper(this); QGridLayout *gridLayout = new QGridLayout; for (int i = 0; i < n; ++i) { QLineEdit *lineEdit = new QLineEdit(); /*ERROR*/connect(lineEdit, &QLineEdit::editingFinished, signalMapper, &QSignalMapper::map); signalMapper->setMapping(lineEdit, i); gridLayout->addWidget(lineEdit, 0, i); } connect(signalMapper, &QSignalMapper::mappedInt, this, &LineEditYarnSetpoint::editingFinished); setLayout(gridLayout); }
Thanks in advance! :)
-
@t-vanbesien said in no matching member function for call to 'connect':
/*ERROR*/connect(lineEdit, &QLineEdit::editingFinished, signalMapper, &QSignalMapper::map);
void QSignalMapper::map(QObject *sender) expects a
QObject *sender
parameter. void QLineEdit::editingFinished() does not provide one.OIC, void QSignalMapper::map() does not take any parameter. But now there are two slots of the same name which differ only by parameter. Then you may need to use
qOverload<>()
/QOverload<>::of()
to specify which one? MaybeQOverload<>::of<>(&QSignalMapper::map)
orQOverload<>::of<void>(&QSignalMapper::map)
? (I don't know how you specify "the one with no parameter").Ah, see https://stackoverflow.com/questions/68552698/connecting-qsignalmapper-qt-5-15-2 ?
Also, if you had copied and pasted the full error details from the compiler I think it would have said a bit more than just
no matching member function for call to 'connect'
, does it not show information about the various possible slot method overloads? -
Here is the full error message:
/Users/thomasvanbesien/IHM_Bobink/lineedityarnsetpoint.cpp:14: error: no matching member function for call to 'connect' ../IHM_Bobink/lineedityarnsetpoint.cpp:14:3: error: no matching member function for call to 'connect' connect(lineEdit, &QLineEdit::editingFinished, signalMapper, &QSignalMapper::map); ^~~~~~~ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:181:36: note: candidate function not viable: no known conversion from 'void (QLineEdit::*)()' to 'const char *' for 2nd argument static QMetaObject::Connection connect(const QObject *sender, const char *signal, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:184:36: note: candidate function not viable: no known conversion from 'void (QLineEdit::*)()' to 'const QMetaMethod' for 2nd argument static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:432:41: note: candidate function not viable: no known conversion from 'void (QLineEdit::*)()' to 'const char *' for 2nd argument inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:201:43: note: candidate template ignored: couldn't infer template argument 'Func2' static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:242:13: note: candidate template ignored: couldn't infer template argument 'Func2' connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:287:13: note: candidate template ignored: couldn't infer template argument 'Func2' connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot, ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:233:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot) ^ /Users/thomasvanbesien/Qt/6.4.2/macos/lib/QtCore.framework/Headers/qobject.h:276:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot) ^
-
@t-vanbesien said in no matching member function for call to 'connect':
couldn't infer template argument 'Func2'
I'm not sure, but this is probably the one that relates to your situation.
In any case you need to do the "qoverload" I suggested and is in the SO post. -
Thank you for the help. I was able to make it work, I changed the erroneous line to:
connect(lineEdit, &QLineEdit::editingFinished, signalMapper, QOverload<>::of<>(&QSignalMapper::map));
I'm not completely sure why it works. C++ templates are still a bit fuzzy to me, but I'll post an explanation when I understand exactly what the problem was.
-
@t-vanbesien said in no matching member function for call to 'connect':
connect(lineEdit, &QLineEdit::editingFinished, signalMapper, QOverload<>::of<>(&QSignalMapper::map));
I don't believe that is a working copy/paste, I think you mean
QOverload<>::of(&QSignalMapper::map)
?C++ templates are still a bit fuzzy to me, but I'll post an explanation when I understand exactly what the problem was.
There are 2 possible overloads:
QSignalMapper::map()
&QSignalMapper::map(QObject *)
.
QOverload<>::of(...)
picks the first one (no parameters),QOverload<QObject *>::of(...)
picks the second one. The bit inside<...>
specifies the parameters. It's kind of like a cast. -
@JonB said in no matching member function for call to 'connect':
I don't believe that is a working copy/paste, I think you mean
QOverload<>::of(&QSignalMapper::map)
?No, the line I copy/pasted does compile and run and has the expected behavior.
-
@t-vanbesien
Interesting. Could you tell me/give me a reference to where you got an example or documentation which usesQOverload<...>::of<...>(...)
rather thanQOverload<...>::of(...)
, please? Note the bit I am asking about is the::of<>
having a<...>
segment? -
@JonB
I don't know ? I'm not sure I understand the question. I usedQOverload<...>::of<...>()
because you wrote it in your first answer to this post. I can't find any documentation about it. If you want a working example I can share the class .cpp and .h of the class and the small debug function I made to make sure it is working properly. You can put it in an empty qt project to check.
-
@t-vanbesien said in no matching member function for call to 'connect':
because you wrote it in your first answer to this post.
Damn! So I did, I was guessing at that point! :( Now I know better, from the SO link I gave you :)
I suggest that you try copy/paste the following line to replace yours:
connect(lineEdit, &QLineEdit::editingFinished, signalMapper, QOverload<>::of(&QSignalMapper::map));
Even though your
::of<>(
apparently works, it should be::of(
. Don't confuse yourself by using my guessed-syntax, for when you come back to it. -
I replaced the line so that now it is:
connect(lineEdit, &QLineEdit::editingFinished, signalMapper, QOverload<>::of(&QSignalMapper::map));
And everything is still working properly.
By the way, you wrote 'IOC' in one of your answers, what does it mean ?
-
@t-vanbesien said in no matching member function for call to 'connect':
By the way, you wrote 'IOC' in one of your answers, what does it mean ?
I wrote
OIC
=>O-I-C
=> "Oh I See" :)