Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. 'Connections' object does not listen to changes in target?

'Connections' object does not listen to changes in target?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 335 Views 2 Watching
  • 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    Am I correct in assuming that a Connections object does not re-connect to the new value of target when target 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
    

    https://alaskafamilysystems.com/

    1 Reply Last reply
    0
    • P Offline
      P Offline
      patrickkidd
      wrote on last edited by
      #2

      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.

      https://alaskafamilysystems.com/

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        This should work. It is ok to change the larget. It works in c++. I have not tried with Python.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1

        • Login

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