I hava a Python file ang a QML file. i get value in Python file from QML, and the type of value is QJSValue , i want to convert it to Python list. i don't know how to do it.
following is my code.
test.python
#!/usr/bin/env python
# encoding: utf-8
from PyQt5.QtCore import QUrl, QObject, pyqtSlot,QVariant
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtQml import QJSValue
class MyMain(QObject):
@pyqtSlot(QJSValue)
def get_value(self,value):
print(value,type(value))
if __name__ == '__main__':
path = 'test.qml'
app = QApplication([])
view = QQuickView()
con = MyMain()
context = view.rootContext()
context.setContextProperty("con",con)
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
view.show()
app.exec()
test.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
Button {
text: "click"
onClicked: {
con.get_value([{"name":"a","text":"1"},{"name":"b","text":"2"}])
}
}
I realize this question is extremely old, but I thought I'd post how I came to the solution in case someone else stumbles upon it via Google.
While it isn't technically a direct conversion to a python list, what I found useful was to do myQJSValueInstance.toVariant() which converts the instance to a QVariant. I could then use the object as needed (e.g., iterating through it as if it were a normal python list).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With