update UI component from model

Viewed 49

I am puzzled by the inherent complexity of PySide2 / QML to do the simplest of UI related tasks compared to other frameworks. But now that I am stuck with it, have to go down that rabbit hole.

I have a model.py that contains:

def update_state_to_running(self):
    """
    """
    self._logger.info('MODEL - RUNNING')
    self.VIEW_STATE = State.RUNNING

@Property(bool)
def view_running(self):
    """
    """
    return self.VIEW_STATE == State.RUNNING

and in my view.qml

DynamicText {
    anchors.horizontalCenter: parent.horizontalCenter
    text: qsTr("In Progress")
    visible:{ console.log('RUNNING');
              console.log(model.view_running);
              return model.view_running;}
    font.pixelSize: 18
}

Now, I see the console output when I open the view, the text label does not show - correct. But it does not appear when I run update_state_to_running on my model. In other frameworks this would show the label but it seems I have to trigger an event to update the UI? I am truly puzzled why this is so overly complicated for such a simple task.

1 Answers

A default property is not notifiable, if you want the change to be notified then you must associate it with a signal:

class Foo(QObject):
    stateChanged = Signal()
    # ...
    def update_state_to_running(self):
        """
        """
        self._logger.info('MODEL - RUNNING')
        self.VIEW_STATE = State.RUNNING
        self.stateChanged.emit()

    @Property(bool, notify=stateChanged)
    def view_running(self):
        """
        """
        return self.VIEW_STATE == State.RUNNING
Related