ListView: Is there a signal after the list view has been updated?
-
Hi,
I have a list view which displays a C++ model. After the model is updated (e.g. rows added or removed) I want to use the positionViewAtIndex function the center an item in the listview. To which system should I listen to?
Can I listen to the signals of the model, e.g. endInsertRows? I am aksing because I don't understand the order in which things happen. After the model changed, the list view could need to create new components, so postionViewAtIndex should happen after this happened, right?
-
Hi,
I have a list view which displays a C++ model. After the model is updated (e.g. rows added or removed) I want to use the positionViewAtIndex function the center an item in the listview. To which system should I listen to?
Can I listen to the signals of the model, e.g. endInsertRows? I am aksing because I don't understand the order in which things happen. After the model changed, the list view could need to create new components, so postionViewAtIndex should happen after this happened, right?
@maxwell31 There are many signals for each type of modification(See https://doc.qt.io/qt-5/qabstractitemmodel.html#signals). A possible solution is that you create a custom class where you create a signal that is connected to the other signals and then use that signal in QML:
class FooModel: public XModel{ // ... signals: void fooSignal(); //... }; ```cpp // in constructor connect(this, &FooModel::rowsRemoved, this, &FooModel::fooSignal); connect(this, &FooModel::rowsInserted, this, &FooModel::fooSignal); connect(this, &FooModel::modelReset, this, &FooModel::fooSignal); connect(this, &FooModel::dataChanged, this, &FooModel::fooSignal); // ...
Then
Connections: { target: cppModel onFooSignal: function(){ console.log("Foo"); } }
-
What I meant was rather the following:
Lets assume a model is reset. If I connect to endModelReset, and call positionViewAtIndex, can I be sure, that the ListView already completed constructing the delegates, so that positionViewAtIndex does what it should?
-
No you can't, since the ListView might not even create the delegates if those are of the view.
However I would assume it to work regardless of this.
You could wrap yourpositionViewAtIndex
call in aQt.callLater
to be sure it's not called too soon.