我想使用QQmlListReference来操作QQmlListProperty,这似乎正是它的用途。但即使在一个最小的例子中,我也无法构造一个有效的QQmlListReference
.
自包含示例:
// demo.cpp
#include <QQmlListProperty>
#include <QQmlListReference>
#include <QtTest>
class Demo : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> prop READ prop)
QList<QObject*> m_list;
public:
QQmlListProperty<QObject> prop() { return {this, &m_list}; }
};
class TestDemo : public QObject
{
Q_OBJECT
private slots:
void testProp() {
Demo demo;
QQmlListReference ref(&demo, "prop");
QVERIFY(ref.isValid()); // <= fails here
}
};
QTEST_APPLESS_MAIN(TestDemo)
#include "demo.moc"
而CMakeLists
, 只是为了完整性:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(demo LANGUAGES CXX)
find_package(Qt5 COMPONENTS Qml Test)
add_executable(demo demo.cpp)
set_property(TARGET demo PROPERTY AUTOMOC ON)
target_link_libraries(demo PRIVATE Qt5::Qml Qt5::Test)
关于我做错了什么的任何建议?