简体   繁体   English

无法将QMetaMethod与Lambda连接

[英]cannot connect QMetaMethod with lambda

I have a base class Binded settings to bind property in it with given widgets, like LineEdit in example. 我有一个基类绑定设置,用于使用给定的小部件绑定属性,例如示例中的LineEdit。 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 ^ 如我所见,它与有关如何将QMetaMethod与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: 在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 我在相同的功能中有一些经典的连接(没有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, ={}); C:\\ Projects \\ some \\ settings.cpp:279:错误:没有匹配的函数可以调用'BindedSettings :: connect(BindedSettings *,QMetaMethod&,BindedSettings *,BindedSettings :: bindWtToProp(QLineEdit *,const char *)::) 'connect(this,signal,this,= {});

You are mixing 2 definitions of QObject::connect() : 您正在混合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)

But connect() has no overload that takes both a QMetaMethod and a Functor . 但是connect()没有使用QMetaMethodFunctor重载。

This exact same question has already been asked 5 years ago on Qt forum , the answer was: 5年前在Qt论坛上已经问过这个完全相同的问题,答案是:

Connections to functors/lambdas use function pointers. 与函子/ lambda的连接使用函数指针。 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. 我相信情况没有改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM