'Connections' object does not listen to changes in target?
Unsolved
QML and Qt Quick
-
Am I correct in assuming that a
Connections
object does not re-connect to the new value oftarget
whentarget
changes? Surely this is a bug?For example, the following code fails:
test_qml.qml
import QtQuick 2.15 import com.tester.stuff 1.0 Item { id: root property var mine: Mine {} property var theirs: null onMineChanged: print('onMineChanged:', mine) Connections { target: mine function onYoursChanged() { print('onYoursChanged', mine.yours) } } Connections { // this connects to the first value of &.yours, // but does not switch to new values of &.yours, // even when `mine.yoursChanged` is emitted. target: mine.yours function onTheirsChanged() { // never called print('onTheirsChanged', theirs) root.theirs = mine.yours.theirs } } }
test_qml.py
from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtQml import * from PyQt5.QtQuick import * from PyQt5.QtQuickWidgets import * class Yours(QObject): theirsChanged = pyqtSignal(int) def __init__(self, parent, theirs): super().__init__(parent) self._theirs = theirs @pyqtProperty(int, notify=theirsChanged) def theirs(self): return self._theirs class Mine(QObject): yoursChanged = pyqtSignal(QVariant) def __init__(self, parent=None): super().__init__(parent) self._yours = Yours(self, -1) @pyqtProperty(QVariant, notify=yoursChanged) def yours(self): return self._yours def setYours(self, x): self._yours = Yours(self, x) self.yoursChanged.emit(self._yours) qmlRegisterType(Mine, "com.tester.stuff", 1, 0, "Mine") app = QApplication([]) engine = QQmlEngine() searchView = QQuickWidget() searchView.setSource(QUrl.fromLocalFile("test_qml.qml")) mine = searchView.rootObject().property('mine') mine.setYours(1) assert searchView.rootObject().property('theirs') == mine.yours.theirs mine2 = Mine() mine2.setYours(2) searchView.rootObject().setProperty('mine', mine2) # `Connections` doesn't update to the new value of mine.yours assert searchView.rootObject().property('theirs') == mine2.yours.theirs
-
Just looping back here to see if anyone has any ideas. Maybe a pattern to avoid this behavior? It would be great to have some kind of global context property that can be used in
Connection
object and who's value can change. -
This should work. It is ok to change the larget. It works in c++. I have not tried with Python.