[英]Qt compiler error when trying to use QQmlListProperty
我正在尝试使用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);
遇到编译器错误return QQmlListProperty<QObject>(this, &m_vurms);
:
main.cpp:20: error: C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'QQmlListProperty<QObject>'
我也尝试用QList替换Vurm的QList - 问题似乎在Qt在QQmlListProperty<T>(this, &m_vurms);
做的任何事情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
的构造QQmlListProperty
需要非const指针。 当您尝试删除它时可能仍然存在错误,因为您仍在传递指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.