Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Button onclick pass json list to python function?



  • 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 )
    }
    }
    

    main.py

    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!



  • @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....


  • Lifetime Qt Champion

    Hi,

    I haven't played with QML and Python be beside the suggestion of @JonB, I would have the slot signature use QVariantMap.



  • 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


Log in to reply