Is there any state management library for PySide project?
-
We know that when developing PySide projects, we generally use the MVC (Model - View - Controller) architectural pattern. However, a problem may arise here:
Different components have their own Models. If the Model of Component A and the Model of Component B are based on the same underlying data, and the Model of Component A is modified, then how can the Model of Component B be updated simply and conveniently in real - time?
In front - end development, for example, in React, there are third - party libraries like Zustand for state management that can solve such problems. But are there similar libraries during the PySide development process?
-
@markleo said in Is there any state management library for PySide project?:
and the Model of Component A is modified, then how can the Model of Component B be updated simply and conveniently in real - time?
At least for the simple case you show the Qt way would be to attach slots to modify Model B on the signals emitted by Model A whenever it undergoes modification. Those include e.g.
QAbstractItemModel::dataChanged()
,rowsInserted()
,rowsRemoved()
and others at https://doc.qt.io/qt-6/qabstractitemmodel.html#signals.If you then wish to do the reverse as well --- modify A whenever B is changed --- you could then also attach the signals/slots in the opposite direction. You must then be careful not to allow an endless "ping-pong" of modifications in both directions.
If this gets too much (e.g. many models) then in answer to your original question Qt state management is supplied by The State Machine Framework and the QStateMachine Class.
All of the above are available from PySide.
-
Qt is not strictly MVC, the difference between Model and Controller can be blurred quite a lot.
There can also be multiple layer of "models".In your example, can both your views share a common model? Alternatively could you have two models sharing some common underlying data?
Unless you need generic signal/slot logic (for generic models like proxy models for example), I'd refrain from using
QAbstractItemModel
signals and use more specific ones instead if you need signal/slot communication.For example if you have a todo list model, I'd recommend having a
todoItemAdded
signal instead of relying onrowsInserted
.From what I understand Qt is more granular that state management libraries like Zustand or similars, at least for the notification changes part. Qt has QAbstractItemModel with its various change signals but there's also properties with notify signals for individual QObjects. You could mix both like https://github.com/OlivierLDff/ObjectListModel/ is doing by exposing updateable roles for each property of its list of QObject derived objects.
Qt has automatic property bindings in QML, offering a similar (and one can argue nicer) syntax than React and friends.Ultimately there's no one size fit all solution to this and it depends on the actual "model" (model can mean a QAbstractItemModel here but also other objects) you want.
-
@GrecKo said in Is there any state management library for PySide project?:
In your example, can both your views share a common model? Alternatively could you have two models sharing some common underlying data?
@markleo
Following up this suggestion from @GrecKo. If the models really share the same data you could have that as a single, source model and then impose separate proxy models on top of that for each distinct view. But that relies on a common, shared, base model, not suitable if the models are really quite distinct.