无法将 QMetaMethod 与 lambda 连接起来

cannot connect QMetaMethod with lambda

我有一个基础 class 绑定设置,可以在其中绑定 属性 给定的小部件,例如示例中的 LineEdit。我坚持使用连接信号和插槽。正如我所见,它与 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){...}
}

在 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;
}

我在同一个函数中有一些 classic 连接(没有 qmetamethod),但我得到的是

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, ={});

您正在混合 QObject::connect() 的 2 个定义:

  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)

但是 connect() 没有同时接受 QMetaMethodFunctor 的重载。

这个完全相同的问题已经在 5 年前在 Qt forum 上被问过,答案是:

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.

我相信情况没有改变。