Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Button onclick pass json list to python function?
Forum Updated to NodeBB v4.3 + New Features

Button onclick pass json list to python function?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
4 Posts 3 Posters 856 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jay mainpy
    wrote on 2 Jun 2021, 11:45 last edited by
    #1

    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!

    J 1 Reply Last reply 2 Jun 2021, 14:29
    0
    • J jay mainpy
      2 Jun 2021, 11:45

      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!

      J Online
      J Online
      JonB
      wrote on 2 Jun 2021, 14:29 last edited by JonB 6 Feb 2021, 14:29
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 2 Jun 2021, 19:01 last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jay mainpy
          wrote on 4 Jun 2021, 09:50 last edited by
          #4

          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 Reply Last reply
          1

          1/4

          2 Jun 2021, 11:45

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved