Trying to Connect QML Signal to PySide6 Slot using QQuickWidget
-
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 QQuickViewI am changing the main.py as follows. This does not trigger the slot even though there is no errors.
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 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 QQuickViewI am changing the main.py as follows. This does not trigger the slot even though there is no errors.
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 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 QQuickViewI am changing the main.py as follows. This does not trigger the slot even though there is no errors.
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 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())
-