2017-04-02 48 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