Button onclick pass json list to python function?
-
wrote on 2 Jun 2021, 11:45 last edited by
i'm trying to save all user input to all the GUI text fields/checkboxes to a .json file. For that i manually selected all the textfields i'd like to save (in the code below ids are textfield1 and 2...btw is there a way in qml to select all text input fields?) and now i'd like to pass it to my controller to save it to a file. I haven't found a good working example of this anywhere and would be thankful for hints...or call me out if i'm doing something stupid :D
main.qml:
Button { ... onClicked: { var v = {"textfield1entry": textfield1.text, "textfield2entry": textfield2.text} controller.saveSettings( v ) } }
from PyQt5 import QtCore, QtGui, QtQml ... class controller(QtCore.QObject): ... some working functions for buttons here... @QtCore.pyqtSlot(list) def saveSettings(self, saveditems): print(saveditems)
for now i just want to see the json type output as print but all i get is [] an empty list. do i have the wrong pyqtSlot argument?
Thank you for your answer in advance!
-
i'm trying to save all user input to all the GUI text fields/checkboxes to a .json file. For that i manually selected all the textfields i'd like to save (in the code below ids are textfield1 and 2...btw is there a way in qml to select all text input fields?) and now i'd like to pass it to my controller to save it to a file. I haven't found a good working example of this anywhere and would be thankful for hints...or call me out if i'm doing something stupid :D
main.qml:
Button { ... onClicked: { var v = {"textfield1entry": textfield1.text, "textfield2entry": textfield2.text} controller.saveSettings( v ) } }
from PyQt5 import QtCore, QtGui, QtQml ... class controller(QtCore.QObject): ... some working functions for buttons here... @QtCore.pyqtSlot(list) def saveSettings(self, saveditems): print(saveditems)
for now i just want to see the json type output as print but all i get is [] an empty list. do i have the wrong pyqtSlot argument?
Thank you for your answer in advance!
wrote on 2 Jun 2021, 14:29 last edited by JonB 6 Feb 2021, 14:29@jay-mainpy
I am replying to this because nobody else has done so far. But I know very little about QML! Perhaps someone who does know will comment further. It is possible you would get a better response if you posted this in the QML sub-forum rather than the Python one.My observation is that your data structure in JSON/JavaScript is not what i would expect for a list, including in Python. Rather it is an object (with fields/attributes). I don't know whether
@QtCore.pyqtSlot(list)
forces Python/PyQt5 to treat it as a list, is it any better if you try@QtCore.pyqtSlot(object)
?It may well turn out this is not the issue, but worth a try. I believe there are many posts about what you might have to do to pass a parameter from QML/JavaScript to C++ or Python, so the problem might lie there instead....
-
Hi,
I haven't played with QML and Python be beside the suggestion of @JonB, I would have the slot signature use QVariantMap.
-
wrote on 4 Jun 2021, 09:50 last edited by
Thanks @JonB and @SGaist for your suggestions, they led me to the right path. I tried to find a QVariantMap signature for QtPy5 but couln't find any. But after a bit more googeling i found QVariant which returned an Qt.Qml.QJSValue object.... which after quite a bit more googeling has to be transformed again with .toVariant() to be accessible with python. I don't fully grasp why, but it works now :)
So for anyone stumbling across this on their google journey: main.qml stays the same as above to pass a json object. But in Python you need:
main.py:from PyQt5 import QtCore, QtGui, QtQml ... class controller(QtCore.QObject): ... some working functions for buttons here... @QtCore.pyqtSlot(QtCore.QVariant) def saveSettings(self, saveditems): saveditems2 = saveditems.toVariant() print(saveditems2["textfield1entry"])
which prints the value of: textfield1.text
1/4