Issue
I need to generate a list of tuples on the python side, and send it to the qml and prints it out, but when I trying to prints out this list on the qml side it prints:
qml: [QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject),QVariant(PyQt_PyObject)]
How can I unpack this values?
UPD
Minimal example:
main.py
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
class Plot(QObject):
def __init__(self):
QObject.__init__(self)
updCanv = pyqtSignal(list, arguments=['upd'])
@pyqtSlot()
def upd(self):
points = [(1, 2), (3, 4)]
self.updCanv.emit(points)
if __name__ == "__main__":
import sys
sys.argv += ['--style', 'material']
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
plot = Plot()
engine.rootContext().setContextProperty("plot", plot)
engine.load("main.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
main.qml
import QtQuick 2.0
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.2
ApplicationWindow {
visible: true
Button {
text: qsTr("Get points")
onClicked: plot.upd()
}
Connections {
target: plot
onUpdCanv: print(upd)
}
}
Solution
Not all types are mapped in QML, for example the type list of python is mapped as the list type of QML, but in the case of the tuple it is not, so the solution is to use some equivalent type or maybe create some new type, in this case you could easily use QPointF()
since the tuple has 2 elements.
class Plot(QObject):
updCanv = pyqtSignal(list, arguments=['upd'])
@pyqtSlot()
def upd(self):
points = [(1, 2), (3, 4)]
p = [QPointF(*v) for v in points]
self.updCanv.emit(p)
Output:
qml: [QPointF(1, 2),QPointF(3, 4)]
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.