分配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)。任何想法可能會造成這種情況?
的Qt 5.4,2014年我是否理解正確的話,您使用的是一些git快照?你的代碼是否適用於5.3.1? – Drop 2014-09-06 09:17:23
是的,我從源頭上構建。我嘗試了5.3,不幸的是它也是同樣的錯誤。 – Mitch 2014-09-06 09:22:01
在QML中看起來問題是'inventory:window.inventory'。斷言是由於'window.inventory'爲空而觸發的。 – mcchu 2014-09-06 12:35:16