在QML

2014-09-06 57 views
0

分配null以C++類的QObject *屬性時,我去掉下面的一行時斷言失敗,我得到一個斷言:在QML

ASSERT:!」 from.isNull()& & to.isNull ()」在文件[...] \ qtdeclarative \ SRC \ QML \ QML \ qqmlpropertycache.cpp,線1586

#include <QGuiApplication> 
#include <QtQml> 
#include <QtQuick> 

class Inventory : public QObject 
{ 
    Q_OBJECT 
public: 
    Inventory() { 
    } 
}; 

class InventoryModel : public QObject 
{ 
    Q_OBJECT 
    Q_PROPERTY(Inventory *inventory READ inventory WRITE setInventory NOTIFY inventoryChanged) 
public: 
    InventoryModel() : mInventory(0) { 
    } 

    Inventory *inventory() const { 
     return mInventory; 
    } 

    void setInventory(Inventory *inventory) { 
     if (inventory != mInventory) { 
      mInventory = inventory; 
      emit inventoryChanged(); 
     } 
    } 
signals: 
    void inventoryChanged(); 
private: 
    Inventory *mInventory; 
}; 

int main(int argc, char *argv[]) 
{ 
    QGuiApplication app(argc, argv); 

// qRegisterMetaType<Inventory*>(); 
    qmlRegisterType<InventoryModel>("Qml", 1, 0, "InventoryModel"); 

    QQmlApplicationEngine engine; 
    engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); 

    return app.exec(); 
} 

#include "main.moc" 

main.qml:

import QtQuick 2.3 
import QtQuick.Controls 1.2 

import Qml 1.0 

ApplicationWindow { 
    id: window 
    visible: true 
    width: 640 
    height: 480 

    InventoryModel { 
     inventory: null 
    } 
} 

我在用Qt 5.4編譯(qt聲明是在f9ee33f9683a4cd4d1a2e41efa6e8d124e9d731d)。任何想法可能會造成這種情況?

+0

的Qt 5.4,2014年我是否理解正確的話,您使用的是一些git快照?你的代碼是否適用於5.3.1? – Drop 2014-09-06 09:17:23

+0

是的,我從源頭上構建。我嘗試了5.3,不幸的是它也是同樣的錯誤。 – Mitch 2014-09-06 09:22:01

+0

在QML中看起來問題是'inventory:window.inventory'。斷言是由於'window.inventory'爲空而觸發的。 – mcchu 2014-09-06 12:35:16

回答

2

嘗試qmlRegisterType代替:

qmlRegisterType<Inventory>("Qml", 1, 0, "Inventory"); 

斷言可能尚未公佈,爲9月6日的與此有關bug

+0

這不是事實。例如,我一直在註冊沒有問題的類型指針,並且Qt代碼也是如此:'git grep「qRegisterMetaType <.*\*>」'在qtbase.git中。 – Mitch 2014-09-06 15:28:29

+0

我的不好,請看看更新的答案:) – fxam 2014-09-06 15:54:11

+0

謝謝。 :)奇怪的是,谷歌搜索沒有顯示出該錯誤 - 這是一個很好的提示,首先搜索錯誤報告。我現在得到了用這種方法退出時出現的雙重刪除問題,但這在我的代碼中很可能是一個問題。 – Mitch 2014-09-06 16:04:50