我正在尝试使用 QQmlListProperty 从 QQuickItem 中公开 QList - 并遵循以下文档:
- 具有对象列表类型的属性
- QQmlList属性类
一个简化的示例:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QList>
#include <QQmlListProperty>
class GameEngine : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> vurms READ vurms)
public:
explicit GameEngine(QQuickItem *parent = 0) :
QQuickItem(parent)
{
}
QQmlListProperty<QObject> vurms() const
{
return QQmlListProperty<QObject>(this, &m_vurms);
}
protected:
QList<QObject*> m_vurms;
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
return app.exec();
}
#include "main.moc"
但是我在return QQmlListProperty<QObject>(this, &m_vurms);
上收到编译器错误:
main.cpp:20: error: C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'QQmlListProperty<QObject>'
我还尝试用 int 的 QList 替换 Vurm 的 QList - 问题似乎出在 Qt 在QQmlListProperty<T>(this, &m_vurms);
所做的一切
我正在使用Qt 5.8编写/编译,C++11在.pro文件中设置。我正在Windows 10上的Qt Creator 4.2.1中编译:使用MSVC 2015 64位进行编译。
我之前错过了这一点,但是您需要将引用作为第二个参数传递给构造函数,而不是指针:
QQmlListProperty<Vurm> GameEngine::vurms()
{
return QQmlListProperty<Vurm>(this, m_vurms);
}
我还必须删除 const
限定符才能让它编译,这是有道理的,因为 QQmlListProperty
的构造函数需要一个非常量指针。当您尝试删除它时,错误可能仍然存在,因为您仍在传递指针。