【发布时间】:2023-02-25 07:38:13
【问题描述】:
QProperty::onValueChange() 返回一个 QPropertyChangeHandler 对象,我认为应该将其存储为成员变量,这样它就不会被销毁并因此注销 Functor。
由于 QPropertyChangeHandler 是一个采用 Functor 的模板类,我需要提供我将要使用的 Functor 的类型,但我不确定在那里放置什么类型。
我应该如何存储返回的QPropertyChangeHandler对象?
以下是我迄今为止最好的尝试:
测试.h
#ifndef PROP_TEST
#define PROP_TEST
#include <QObject>
#include <QProperty>
#include <QString>
#include <QPropertyChangeHandler>
#include <functional>
class PropTest : public QObject {
Q_OBJECT
public:
QProperty<QString> m_property;
PropTest();
~PropTest() = default;
// QString m_property;
QPropertyChangeHandler< std::function<void()> > propertyChangeHandler;
void TestFunction();
};
#endif
Proptest.cpp
#include <QObject>
#include <QString>
#include <QDebug>
#include <QApplication>
#include "prop_test.h"
PropTest::PropTest() { // <- error occurs here
this->propertyChangeHandler = this->m_property.onValueChanged(&PropTest::TestFunction);
}
void PropTest::TestFunction() {
// further processing
}
int main(int arc, char* argv[]) {
auto app = QApplication(arc, argv);
auto ob = new PropTest{};
ob->m_property = "String";
ob->m_property = "New";
app.exec();
}
但我得到的只是这个不熟悉的错误:
[build] prop_test.cpp: In constructor 'PropTest::PropTest()':
[build] prop_test.cpp:11:20: error: no matching function for call to 'QPropertyChangeHandler<std::function<void()> >::QPropertyChangeHandler()'
[build] 11 | PropTest::PropTest() {
[build] | ^
我发现错误发生的地方很奇怪。
【问题讨论】:
-
乍一看,您似乎已将成员变量声明为采用带有 1
bool参数的函数。您的TestFunction方法采用参数,因此它将进入std::function<void()>。这是你要问的吗? -
很抱歉造成混淆,这一定是准备 MRE 时的错字。