Failed to connect signal in Python (using Pyside) to signal (work as slot) in QML, What is QVarient equivalent in PySide ?
Solved
Language Bindings
-
Currently, I have a signal in QML file like this:
signal fromThread (string rev_mess, var rev_data) onFromThread: { }
I need to create a correct Signal in python to connect to fromThread in QML file.
I tried this:class Bico_QUIThread(QThread): toUI = Signal() def __init__(self): self.toUI.connect(self._engine.rootObjects()[0].fromThread)
But what I got is runtime error :
RuntimeError: Failed to connect signal toUI().
I know the reason is the argument's type of toUI and fromThread not matched.
toUI must be some thing like:toUI = Signal(str, QVariant)
But QVarriant is no longer support in Pyside.
So what other solutions I can do ?
Thank you a lot in advance.
Full code:
Python code:import sys from PySide6.QtCore import Signal, Slot from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtCore import QThread class Bico_QUIThread(QThread): toUI = Signal() def __init__(self): QThread.__init__(self) self.engine = QQmlApplicationEngine() self.engine.load("C:/Users/Truc/Desktop/Bico_QUIThread_Sample.qml") self.toUI.connect(self.engine.rootObjects()[0].fromThread) if __name__ == "__main__": app = QGuiApplication(sys.argv) thr = Bico_QUIThread() thr.start() sys.exit(app.exec())
.
QML code:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { id: window objectName: "window" width: 640 height: 480 visible: true title: qsTr("Hello World") signal fromThread(string rev_mess, var rev_data) onFromThread: { if (rev_mess == "size") { button.height = rev_data.height button.width = rev_data.width } else { console.log(rev_mess + " " + rev_data) } } }
-
Hi,
Good question but sadly I don't have definitive answer to it. However, did you already try to use the Any type from the typing module ?
-
The problem was solved.
Replace QVariant -> "QVariant"
The correct definition of the signal in python code is:toUI = Signal(str, "QVariant")