c++qtsignals-slotsqmetaobject

cannot connect QMetaMethod with lambda


I have a base class Binded settings to bind property in it with given widgets, like LineEdit in example. I stuck with connecting signals and slot. As i see it's the same as provided code in answer on How to use QMetaMethod with QObject::connect ^

class BindedSettings: public QObject
{
    Q_OBJECT
public:
bool bindWtToProp(QLineEdit* targetWt, const char* propertyName);
bool stringFromVariant(const QVariant& val, QString& result){...}
}

in cpp:

bool BindedSettings::bindWtToProp(QLineEdit *targetWt, const char *propertyName)
{
    QLineEdit* le = targetWt;
    QMetaProperty mp = metaObject()->property(metaObject()->indexOfProperty(propertyName));

    //connecting property notifiedSignal with reader lambda
    QMetaMethod signal = mp.notifySignal();
    connect(this, signal, this, [=](){
    }); //reader
    return true;
}

I have some classic connections(without qmetamethod) in the same function, but here what i get is

C:\Projects\some\settings.cpp:279: error: no matching function for call to 'BindedSettings::connect(BindedSettings*, QMetaMethod&, BindedSettings*, BindedSettings::bindWtToProp(QLineEdit*, const char*)::)' connect(this, signal, this, ={});


Solution

  • You are mixing 2 definitions of QObject::connect():

    1. QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
    2. QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)

    But connect() has no overload that takes both a QMetaMethod and a Functor.

    This exact same question has already been asked 5 years ago on Qt forum, the answer was:

    Connections to functors/lambdas use function pointers. They need to be resolved at compile-time, because the compiler needs to know what types of function pointers you are using. You can't use runtime strings.

    I believe the situation has not changed.