我正在使用QtQuick 2.0和QML ListView,它们已用C ++(对象的QList)连接到我的模型。通过QQmlContext :: setContextProperty()建立连接。
现在文档告诉我接口没有直接的方式来了解更改,因此,仅在更改模型时才实现上下文。但是,当我这样做时,视图将直接实现而不触发任何事件(例如添加或删除事件),这使我有些恼火,因为我无法控制过渡。
简单地说,这是我的qml代码:
ListView {
id : list
boundsBehavior: Flickable.StopAtBounds
anchors {
top: titleBar.bottom
topMargin: -1
bottom: mainWindow.bottom
bottomMargin: -1
}
width: mainWindow.width
model: episodes
delegate: Episode {
id: myDelegate
onShowClicked: episodes.append(episodes[index])
}
ScrollBar {
flickable: list;
}
}
其中Episode是我的自定义代表。它包含以下代码:
ListView.onAdd: SequentialAnimation {
PropertyAction { target: episodeDelegate; property: "height"; value: 0 }
NumberAnimation { target: episodeDelegate; property: "height"; to: 80; duration: 250; easing.type: Easing.InOutQuad }
}
ListView.onRemove: SequentialAnimation {
PropertyAction { target: episodeDelegate; property: "ListView.delayRemove"; value: true }
NumberAnimation { target: episodeDelegate; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
// Make sure delayRemove is set back to false so that the item can be destroyed
PropertyAction { target: episodeDelegate; property: "ListView.delayRemove"; value: false }
}
这是Qt示例的直接副本。
总而言之,模型已正确链接和同步,尽管这样做使我无法了解QML逻辑中模型更改的性质。
有人知道有什么把戏吗?
重置时setContextProperty
,您可以使用populate
过渡。但是,这会将转换同时应用于列表中的所有元素。
如果您希望每次添加一个项目都具有动画效果,则可以使用信号来实现。例如:
class SomeList : public QObject
{
Q_OBJECT
public:
explicit SomeList(QObject *parent = 0);
void Add(QString color, QString value)
{
emit addNew(color,value);
}
signals:
void addNew(QString data1,QString data2);
};
在main.cpp中,您可以拥有:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("cppInstance",new SomeList);
return app.exec();
}
在QML中:
ListModel{
id:someListModel
}
Rectangle{
width: 600
height: 600
ListView{
model:someListModel
delegate:Rectangle{
width: parent.width
height: parent.height/10
color: model.color
Text{
text: value
}
}
}
Connections{
target: cppInstance
onAddNew: { someListModel.insert(0,{"color":data1,"value":data2})}
}
}
在SomeList
该类中,您还可以有一个QList
as成员,其中将包含您在QML中插入的字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句