qt - QML ListView填充动画不起作用
问题描述
我有一个 C++ 模型QAbstractListModel
,我在 QML 中使用它ListView
ListView {
id: listView
populate: Transition {
id: addTrans
SequentialAnimation {
PauseAnimation {
duration: (addTrans.ViewTransition.index -
addTrans.ViewTransition.targetIndexes[0]) * 100
}
NumberAnimation { property: "scale"; from: 0; to: 1 }
}
}
anchors.fill: parent
delegate: listDelegate
model: newsModel
}
我尝试为填充列表时添加动画,但它不起作用。所以我所做的是我尝试更改populate
为add
并触发动画,但是问题是动画开始时每个项目都已经在列表中:换句话说,不是有一个添加项目的空列表,我有一个完整的元素列表,然后单独动画。我也尝试过遵循这个建议,但它不起作用。
编辑:根据要求,我提供了一个最小的工作示例来重现我的问题。该问题可以通过以下方式重现:
#include <QObject>
#include <QQmlObjectListModel.h>
class Item : public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
public:
Item(QObject* parent = nullptr): QObject(parent)
{
}
QString name() const{
return mName;
}
void setName(QString const &name){
if(mName != name){
mName = name;
emit nameChanged();
}
}
signals:
void nameChanged();
private:
QString mName;
};
class DataManager : public QObject{
Q_OBJECT
Q_PROPERTY(QQmlObjectListModel<Item>* itemsModel READ itemsModel CONSTANT)
public:
DataManager(){
mItemsModel = new QQmlObjectListModel<Item>(this);
qRegisterMetaType<QQmlObjectListModel<Item>*>("QQmlObjectListModel<Item>*");
// I have also tried to add items from here, but the result is even worse
// for(int i = 0; i < 20; i++){
// addItem(QString::number(i));
// }
}
QQmlObjectListModel<Item>* itemsModel(){
return mItemsModel;
}
Q_INVOKABLE void addItem(QString const &name){
Item* item = new Item(mItemsModel);
item->setName(name);
mItemsModel->append(item);
}
private:
QQmlObjectListModel<Item>* mItemsModel;
};
然后在qml中:
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Scroll")
ScrollView {
anchors.fill: parent
ListView {
width: parent.width
// dataManager is a context property set via c++
model: dataManager.itemsModel
populate: Transition {
id: popTrans
SequentialAnimation {
PauseAnimation {
duration: (popTrans.ViewTransition.index -
popTrans.ViewTransition.targetIndexes[0]) * 100
}
NumberAnimation { property: "scale"; from: 0; to: 1 }
}
}
add: Transition {
id: addTrans
SequentialAnimation {
PauseAnimation {
duration: (addTrans.ViewTransition.index -
addTrans.ViewTransition.targetIndexes[0]) * 100
}
NumberAnimation { property: "scale"; from: 0; to: 1 }
}
}
delegate: ItemDelegate {
text: "Item" + name
width: parent.width
}
}
}
Component.onCompleted: {
var p;
for(p=0; p < 20; p++){
dataManager.addItem(p)
}
}
}
这是我从这里QQmlObjectListModel
借来的助手类
解决方案
我不确定您到底想要什么,但我希望我的代码对您有所帮助。
populate: Transition {
id: addTrans
SequentialAnimation {
PropertyAction {
property: "visible"
value: false
}
PauseAnimation {
duration: (addTrans.ViewTransition.index -
addTrans.ViewTransition.targetIndexes[0]) * 100
}
PropertyAction {
property: "visible"
value: true
}
NumberAnimation { property: "scale"; from: 0; to: 1 }
}
}
推荐阅读
- python - 为什么我在 github 上的 python 包没有显示 used by 信息?
- python - ValueError:检查输入时出错:预期 left_input 的形状为 (134,),但得到的数组的形状为 (1,)
- image - 如何找到图像中点之间的距离?
- c++ - 如何检查 GLFW 窗口是否正在运行?
- r - 如何自定义 ggplot2 箱线图的颜色?
- visual-studio - 我应该将哪个文档用于 Visual Studio 扩展开发?
- python - 独立运行python测试模块
- java - 我第一次插入 sqlite 会发生什么?
- android - 添加通知通道以在更高版本的 Android 中显示通知
- android - 是否存在首选仍然后台服务的情况?