我正在尝试使用 QQmlListProperty 从 QQuickItem 中公开 QList - 并遵循以下文档:
一个简化的例子:
#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 编写/编译,并且在 .pro 文件中设置了 C++11。我在 Windows 10 上的 Qt Creator 4.2.1 中编译:使用 MSVC 2015 64 位进行编译。
最佳答案
我之前错过了这个,但是你需要将引用作为第二个参数传递给构造函数,而不是指针:
QQmlListProperty<Vurm> GameEngine::vurms()
{
return QQmlListProperty<Vurm>(this, m_vurms);
}
我还必须删除 const
限定符才能使其编译,这是有道理的,因为 QQmlListProperty
的构造函数需要一个非常量指针。当您尝试删除它时错误可能仍然存在,因为您仍在传递指针。
关于c++ - 尝试使用 QQmlListProperty 时出现 Qt 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163602/