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. Trying to Connect QML Signal to PySide6 Slot using QQuickWidget
Forum Updated to NodeBB v4.3 + New Features

Trying to Connect QML Signal to PySide6 Slot using QQuickWidget

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 1 Posters 532 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.
  • I Offline
    I Offline
    iamontherun
    wrote on last edited by iamontherun
    #1

    I am using PySide6 and trying to convert following code example from QT that uses QQuickView to use QQuickWidget

    Original QT Example - https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/qml/signals/qmltopy4
    Orignal Example works well for QQuickView

    I am changing the main.py as follows. This does not trigger the slot even though there is no errors.

    main.py

    import os
    from pathlib import Path
    import sys
    from PySide6.QtCore import QUrl, Slot
    from PySide6.QtQuickWidgets import QQuickWidget
    from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QMainWindow
    
    
    @Slot(float)
    def sayThis(s):
        print(s)
    
    class MainApp(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.initUI()
        
        def initUI(self):
            layout = QVBoxLayout()
            self.wt = QQuickWidget()
            ml_file = os.fspath(Path(__file__).resolve().parent / 'view1.qml')
            self.wt.setSource(QUrl.fromLocalFile(ml_file))
    
            root = self.wt.rootObject()
            root.textRotationChanged.connect(sayThis)
    
    
            layout.addWidget(self.wt)
            self.setLayout(layout)
    
            container = QWidget()
            container.setLayout(layout)
            self.setCentralWidget(container)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainApp()
        window.show()
        sys.exit(app.exec())
    
    

    view1.qml

    import QtQuick
    
    Rectangle {
        id: page
    
        signal buttonClicked
        signal textRotationChanged(double rot)
    
        width: 500; height: 200
        color: "lightgray"
    
        Text {
            id: helloText
            text: "Hello world!"
            y: 30
            x: page.width/2-width/2
            font.pointSize: 24; font.bold: true
            onRotationChanged: textRotationChanged(rotation)
    
            states: State {
                name: "down"; when: buttonMouseArea.pressed === true
                PropertyChanges {
                    target: helloText;
                    rotation: 180;
                    y: 100;
                }
            }
    
            transitions: Transition {
                from: ""; to: "down"; reversible: true
                ParallelAnimation {
                    NumberAnimation {
                        properties: "y,rotation"
                        duration: 500
                        easing.type: Easing.InOutQuad
                    }
                }
            }
        }
    
        Rectangle {
            id: button
            width: 150; height: 40
            color: "darkgray"
            anchors.horizontalCenter: page.horizontalCenter
            y: 120
            MouseArea {
                id: buttonMouseArea
                objectName: "buttonMouseArea"
                anchors.fill: parent
                onClicked: {
                    buttonClicked()
                }
            }
            Text {
                id: buttonText
                text: "Press me!"
                anchors.horizontalCenter: button.horizontalCenter
                anchors.verticalCenter: button.verticalCenter
                font.pointSize: 16
            }
        }
    }
    
    
    I 2 Replies Last reply
    0
    • I iamontherun

      I am using PySide6 and trying to convert following code example from QT that uses QQuickView to use QQuickWidget

      Original QT Example - https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/qml/signals/qmltopy4
      Orignal Example works well for QQuickView

      I am changing the main.py as follows. This does not trigger the slot even though there is no errors.

      main.py

      import os
      from pathlib import Path
      import sys
      from PySide6.QtCore import QUrl, Slot
      from PySide6.QtQuickWidgets import QQuickWidget
      from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QMainWindow
      
      
      @Slot(float)
      def sayThis(s):
          print(s)
      
      class MainApp(QMainWindow):
      
          def __init__(self):
              super().__init__()
              self.initUI()
          
          def initUI(self):
              layout = QVBoxLayout()
              self.wt = QQuickWidget()
              ml_file = os.fspath(Path(__file__).resolve().parent / 'view1.qml')
              self.wt.setSource(QUrl.fromLocalFile(ml_file))
      
              root = self.wt.rootObject()
              root.textRotationChanged.connect(sayThis)
      
      
              layout.addWidget(self.wt)
              self.setLayout(layout)
      
              container = QWidget()
              container.setLayout(layout)
              self.setCentralWidget(container)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          window = MainApp()
          window.show()
          sys.exit(app.exec())
      
      

      view1.qml

      import QtQuick
      
      Rectangle {
          id: page
      
          signal buttonClicked
          signal textRotationChanged(double rot)
      
          width: 500; height: 200
          color: "lightgray"
      
          Text {
              id: helloText
              text: "Hello world!"
              y: 30
              x: page.width/2-width/2
              font.pointSize: 24; font.bold: true
              onRotationChanged: textRotationChanged(rotation)
      
              states: State {
                  name: "down"; when: buttonMouseArea.pressed === true
                  PropertyChanges {
                      target: helloText;
                      rotation: 180;
                      y: 100;
                  }
              }
      
              transitions: Transition {
                  from: ""; to: "down"; reversible: true
                  ParallelAnimation {
                      NumberAnimation {
                          properties: "y,rotation"
                          duration: 500
                          easing.type: Easing.InOutQuad
                      }
                  }
              }
          }
      
          Rectangle {
              id: button
              width: 150; height: 40
              color: "darkgray"
              anchors.horizontalCenter: page.horizontalCenter
              y: 120
              MouseArea {
                  id: buttonMouseArea
                  objectName: "buttonMouseArea"
                  anchors.fill: parent
                  onClicked: {
                      buttonClicked()
                  }
              }
              Text {
                  id: buttonText
                  text: "Press me!"
                  anchors.horizontalCenter: button.horizontalCenter
                  anchors.verticalCenter: button.verticalCenter
                  font.pointSize: 16
              }
          }
      }
      
      
      I Offline
      I Offline
      iamontherun
      wrote on last edited by
      #2

      @iamontherun example i am using is https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/qml/signals/qmltopy3

      1 Reply Last reply
      0
      • I iamontherun

        I am using PySide6 and trying to convert following code example from QT that uses QQuickView to use QQuickWidget

        Original QT Example - https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/qml/signals/qmltopy4
        Orignal Example works well for QQuickView

        I am changing the main.py as follows. This does not trigger the slot even though there is no errors.

        main.py

        import os
        from pathlib import Path
        import sys
        from PySide6.QtCore import QUrl, Slot
        from PySide6.QtQuickWidgets import QQuickWidget
        from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QMainWindow
        
        
        @Slot(float)
        def sayThis(s):
            print(s)
        
        class MainApp(QMainWindow):
        
            def __init__(self):
                super().__init__()
                self.initUI()
            
            def initUI(self):
                layout = QVBoxLayout()
                self.wt = QQuickWidget()
                ml_file = os.fspath(Path(__file__).resolve().parent / 'view1.qml')
                self.wt.setSource(QUrl.fromLocalFile(ml_file))
        
                root = self.wt.rootObject()
                root.textRotationChanged.connect(sayThis)
        
        
                layout.addWidget(self.wt)
                self.setLayout(layout)
        
                container = QWidget()
                container.setLayout(layout)
                self.setCentralWidget(container)
        
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = MainApp()
            window.show()
            sys.exit(app.exec())
        
        

        view1.qml

        import QtQuick
        
        Rectangle {
            id: page
        
            signal buttonClicked
            signal textRotationChanged(double rot)
        
            width: 500; height: 200
            color: "lightgray"
        
            Text {
                id: helloText
                text: "Hello world!"
                y: 30
                x: page.width/2-width/2
                font.pointSize: 24; font.bold: true
                onRotationChanged: textRotationChanged(rotation)
        
                states: State {
                    name: "down"; when: buttonMouseArea.pressed === true
                    PropertyChanges {
                        target: helloText;
                        rotation: 180;
                        y: 100;
                    }
                }
        
                transitions: Transition {
                    from: ""; to: "down"; reversible: true
                    ParallelAnimation {
                        NumberAnimation {
                            properties: "y,rotation"
                            duration: 500
                            easing.type: Easing.InOutQuad
                        }
                    }
                }
            }
        
            Rectangle {
                id: button
                width: 150; height: 40
                color: "darkgray"
                anchors.horizontalCenter: page.horizontalCenter
                y: 120
                MouseArea {
                    id: buttonMouseArea
                    objectName: "buttonMouseArea"
                    anchors.fill: parent
                    onClicked: {
                        buttonClicked()
                    }
                }
                Text {
                    id: buttonText
                    text: "Press me!"
                    anchors.horizontalCenter: button.horizontalCenter
                    anchors.verticalCenter: button.verticalCenter
                    font.pointSize: 16
                }
            }
        }
        
        
        I Offline
        I Offline
        iamontherun
        wrote on last edited by iamontherun
        #3

        I was able to get it to run by moving the root connect code to the bottom of init. after central widget.

        DOES ANYONE KNOW WHY IT WORKS WHEN I ADD SIGNAL CONNECTOR AFTER SETTING THE CENTRAL WIDGET?

        import os
        from pathlib import Path
        import sys
        from PySide6.QtCore import QUrl, Slot
        from PySide6.QtGui import QGuiApplication
        from PySide6.QtQuick import QQuickView
        from PySide6.QtQuickWidgets import QQuickWidget
        from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QMainWindow
        
        @Slot(float)
        def sayThis(s):
            print(s)
        
        class MainApp(QMainWindow):
        
            def __init__(self):
                super().__init__()
                self.initUI()
            
            def initUI(self):
        
                wt = QQuickWidget()
                ml_file = os.fspath(Path(__file__).resolve().parent / 'view1.qml')
                wt.setSource(QUrl.fromLocalFile(ml_file))
                self.setCentralWidget(wt)
        
                self.root = wt.rootObject()
                self.root.textRotationChanged.connect(sayThis)
                
        
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = MainApp()
            window.show()
            sys.exit(app.exec())
        
        
        1 Reply Last reply
        0
        • I iamontherun has marked this topic as solved on

        • Login

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