我有一个基类绑定设置,用于使用给定的小部件(例如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;
}
我在同一函数中有一些经典的连接(没有qmetamethod),但是我得到的是
C:\ Projects \ some \ settings.cpp:279:错误:否 调用的匹配功能 'BindedSettings :: connect(BindedSettings *,QMetaMethod&, BindedSettings *,BindedSettings :: bindWtToProp(QLineEdit *,const char *)::)' connect(this,signal,this,= {});
答案 0 :(得分:2)
您正在混合QObject::connect()
的2个定义:
QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)
但是connect()
不会同时占用QMetaMethod
和Functor
的重载。
5年前在Qt forum上已经问过这个完全相同的问题,答案是:
与函子/ lambda的连接使用函数指针。他们需要解决 编译时,因为编译器需要知道什么类型的 您正在使用的函数指针。您不能使用运行时字符串。
我相信情况没有改变。