Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Is there any state management library for PySide project?
Forum Update on Monday, May 27th 2025

Is there any state management library for PySide project?

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for pythonpysidepython
4 Posts 3 Posters 944 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    markleo
    wrote on 3 Jan 2025, 02:09 last edited by
    #1

    We know that when developing PySide projects, we generally use the MVC (Model - View - Controller) architectural pattern. However, a problem may arise here:

    b2886bb9-c984-49a7-882b-e156e1382e9c-image.png

    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?

    J 1 Reply Last reply 3 Jan 2025, 08:22
    0
    • M markleo
      3 Jan 2025, 02:09

      We know that when developing PySide projects, we generally use the MVC (Model - View - Controller) architectural pattern. However, a problem may arise here:

      b2886bb9-c984-49a7-882b-e156e1382e9c-image.png

      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?

      J Offline
      J Offline
      JonB
      wrote on 3 Jan 2025, 08:22 last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 3 Jan 2025, 08:53 last edited by
        #3

        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 on rowsInserted.

        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.

        J 1 Reply Last reply 3 Jan 2025, 09:26
        1
        • G GrecKo
          3 Jan 2025, 08:53

          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 on rowsInserted.

          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.

          J Offline
          J Offline
          JonB
          wrote on 3 Jan 2025, 09:26 last edited by
          #4

          @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.

          1 Reply Last reply
          0

          2/4

          3 Jan 2025, 08:22

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved