Pyqt : Data Is Not Json Serializable
I am new to PyQt GUI. I want to get the data of a QLineEdit text box, and for that I am using the text() method. I am getting the data, but the data type is a QString. I need to tr
Solution 1:
If you use PyQt5, or Python3 with PyQt4, you will not get this kind of error, because PyQt will always return ordinary Python types whenever possible. But you are using Python2 with PyQt4, which means you have to explicitly request that behaviour.
To do that, change your imports as follows:
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
fromPyQt4.QtCoreimport *
fromPyQt4.QtGuiimport *
(And note that the setapi
calls must go before the first import of any PyQt modules in your application).
With that in place, you will also need to simplify your code to this:
defpersonalData(self):
printtype(self.s1_val.text())
ifnot self.s1_val.text():
print"s1 is empty"#self.popup_window()ifnot self.s2_val.text():
print"s2 is empty"ifnot self.s3_val.text():
print"s3 is empty"ifnot self.s4_val.text():
print"s4 is empty"ifnot self.s5_val.text():
print"s5 is empty"if self.s1_val.text() and self.s2_val.text():
Data1 = {'systemID': self.s1_val.text()}
self.personaldata = json.dumps(Data1)
print ("personal json ready")
PS:
If you're just starting out learning Python and/or PyQt, I would strongly recommend that you use Python3 and PyQt5 if at all possible.
Post a Comment for "Pyqt : Data Is Not Json Serializable"