我有一个 PyQt5 属性与适当的 notify 功能:

class FriendsWidgetBackend(QObject):
    messageCompanionChanged = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent)
        self._current_message_companion = None

    @pyqtProperty('QVariant', notify=messageCompanionChanged)
    def message_companion(self):
        return QVariant(self._current_message_companion)

    @message_companion.setter
    def message_companion(self, message_companion):
        self._current_message_companion = message_companion
        self.messageCompanionChanged.emit()

一切正常,直到我分配给这个变量 undefined - 它在发出 messageCompanionChanged 信号后拒绝更新qml中的变量 . 我甚至试图设置一个断点 - 它从应用程序开始更新到 undefined 赋值:

Item {
    id: rootItem

    property QtObject friendsWidgetBackend

    onVisibleChanged: {
        if (!visible) {
            friendsWidgetBackend.message_companion = undefined
        }
    }
}

经过 C# 的一些经验之后我会说 QML 在这样的任务之后断开了绑定 . 我怎样才能避免这种行为?

附:我的绑定示例:

Column {
    visible: friendsWidgetBackend.message_companion ? true : false
}

P.P.S.即使没有绑定它也不起作用 - 只是试图从QML连接到 messageCompanionChanged 信号 - 它在发射后不会更新值 .

P.P.P.S.如果我将 undefined 更改为空的js对象,如 {}({}) 绑定将起作用,但这不是我需要的 .