2017-04-02 47 views
0

我试图用QQmlListProperty从QQuickItem中暴露出的QList - 并在文档以下内容:Qt的编译器错误时尝试使用QQmlListProperty

一个简化的例子:

#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); GA编译器错误:

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位进行编译。

+0

尝试从功能删除了'const'预选赛。 – Mitch

+0

@米奇 - 不使用时限定在误差保持甚至,当我阅读文档时,常量需要在那里... – HorusKol

+0

我不知道发生了什么事的文档......这是常量的声明,但不在定义中。也许问题是'Vurm' - 你能提供头文件吗? – Mitch

回答

1

我错过了这个较早,但你需要传递的引用,第二个参数的构造函数,而不是一个指针:

QQmlListProperty<Vurm> GameEngine::vurms() 
{ 
    return QQmlListProperty<Vurm>(this, m_vurms); 
} 

我也不得不删除const资格得到它来编译,这这是合理的,因为QQmlListProperty的构造函数需要一个非const指针。当您尝试删除它时,错误可能仍然存在,因为您仍在传递指针。

+0

https://codereview.qt-project.org/#/c/190372/在文档的不匹配。 – Mitch