为QListView的大小调整制作动画

Animate the resizing of a QListView

本文关键字:动画 调整 QListView      更新时间:2023-10-16

我有一个QListView,可以通过调用updateGeometry来适应内容。现在我想设置它的动画。我不能使用resizeEvent,因为它在小部件调整大小后被调用。开始这个动画的正确位置是什么,或者内部调用哪些成员?

好吧,您应该离开QListView,转而关注它的模型。假设你有这样的东西:

QListView *myListView;

在这种情况下,你应该注意它的模型,这意味着:

QAbstractItemModel *myListModel(myListView->model());

您可以连接一些插槽(取决于您想何时开始动画,在视图获取数据之前或之后),可能类似于:

connect(myListModel, &QAbstractItemModel::rowsAboutToBeInserted, myHandlingObject, &MyHandlingObjectClass::myHandlingSlot);

或:

connect(myListModel, &QAbstractItemModel::rowsInserted, myHandlingObject, &MyHandlingObjectClass::myHandlingSlot);

在MyHandlingObjectClass::myHandlingSlot()插槽中,您最终将启动QPropertyAnimation。我想这不算什么。希望它能有所帮助!