gpt4 book ai didi

c++ - 无法将 QMetaMethod 与 lambda 连接起来

转载 作者:行者123 更新时间:2023-11-27 23:42:14 28 4
gpt4 key购买 nike

我有一个基类 Binded 设置,用于将其中的属性与给定的小部件绑定(bind),例如示例中的 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: 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.

我相信情况没有改变。

关于c++ - 无法将 QMetaMethod 与 lambda 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53670400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com