def loadStamps(self): prefs = preferences.Preferences.instance() stampsDirectory = prefs.stampsDirectory() stampsDir = QDir(stampsDirectory) iterator = QDirIterator(stampsDirectory, ["*.stamp"], QDir.Files | QDir.Readable) while (iterator.hasNext()): stampFileName = iterator.next() stampFile = QFile(stampFileName) if (not stampFile.open(QIODevice.ReadOnly)): continue data = stampFile.readAll() document = QJsonDocument.fromBinaryData(data) if (document.isNull()): # document not valid binary data, maybe it's an JSON text file error = QJsonParseError() document = QJsonDocument.fromJson(data, error) if (error.error != QJsonParseError.NoError): qDebug("Failed to parse stamp file:" + error.errorString()) continue stamp = TileStamp.fromJson(document.object(), stampsDir) if (stamp.isEmpty()): continue stamp.setFileName(iterator.fileInfo().fileName()) self.mTileStampModel.addStamp(stamp) index = stamp.quickStampIndex() if (index >= 0 and index < self.mQuickStamps.size()): self.mQuickStamps[index] = stamp
def init(self): #初始化设置默认值 self.highestScore = 0 #最高分 self.highestScorePlayer = "" #最高分保持者 self.themePath = "theme\\default.qss" #主题路径 f = QFile("config.ini") #如果设置文件存在则从中读入设置数据 if f.exists(): #读取文件 f.open(QFile.ReadOnly) b = f.readAll() f.close() #解析json jsonDocument = QJsonDocument.fromJson(b) json = jsonDocument.object() self.highestScore = json["HighestScore"].toInt() #读取最高分 self.highestScorePlayer = json["HighestScorePlayer"].toString( ) #读取最高分保持者 self.themePath = json["ThemePath"].toString() #读取当前主题文件路径 #否则创建设置文件并用默认设置进行游戏 else: f.open(QFile.WriteOnly) f.close() #设置主题 self.setTheme(self.themePath)
def textMessageReceived(self, message): print('textMessageReceived:', message) error = QJsonParseError() json = QJsonDocument.fromJson(message.encode('utf-8', errors='ignore'), error) if error.error: print('Failed to parse message:{}, Error is:{}'.format( message, error.errorString())) return if not json.isObject(): print('Received JSON message that is not an object:{}'.format( message)) return self.messageReceived.emit(json.object(), self)
def loadJsonFromByteArray(self, byteArray: QByteArray): json = QJsonDocument(QJsonDocument.fromJson(byteArray)) topLevelObject = json.object() nodeStyleValues = topLevelObject["FlowViewStyle"] obj = nodeStyleValues.toObject() self.BackgroundColor = self.flowViewStyleReadColor( obj, "BackgroundColor") self.FineGridColor = self.flowViewStyleReadColor(obj, "FineGridColor") self.CoarseGridColor = self.flowViewStyleReadColor( obj, "CoarseGridColor")
def loadJsonFromConsole(self, value): error = QJsonParseError() self.mDocument = QJsonDocument.fromJson(value, error) if self.mDocument is not None: self.beginResetModel() if self.mDocument.isArray(): self.mRootItem.load(list(self.mDocument.array())) else: self.mRootItem = self.mRootItem.load(self.mDocument.object()) self.endResetModel() return True print("QJsonModel: error loading Json") return False
def textMessageReceived(self, messageData): error = QJsonParseError() message = QJsonDocument.fromJson(bytes(messageData)) if error.error: qWarning("Error parsing text message '%s' to JSON object: %s" % (messageData, error.errorString())) return elif not message.isObject(): qWarning("Received JSON message that is not an object: %s", messageData) return print("message:", message, "object:", message.object(), "type: ", type(message.object()), file=sys.stderr) self.messageReceived.emit(message.object(), self)
from TreeModel import TreeModel from reader import read_js if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) # f = QtCore.QFile('default.txt') # f.open(QtCore.QIODevice.ReadOnly) # model = TreeModel(f.readAll()) # f.close() parse_err = QJsonParseError() byte_array = read_js('../example.json') json_document = QJsonDocument.fromJson(byte_array, parse_err) if QJsonParseError.NoError == parse_err.error: print(parse_err.errorString()) # json_document = QJsonDocument() model = TreeModel(json_document) view = QtWidgets.QTreeView() view.setModel(model) view.setWindowTitle("Json Tree Model") view.show() sys.exit(app.exec_())
def forecast_data_received(self): json_str = self._forecast_weather.readAll() json_doc = QJsonDocument.fromJson(json_str) self._read_forecast_data(json_doc.object())
def weather_data_received(self): json_str = self._current_weather.readAll() json_doc = QJsonDocument.fromJson(json_str) self._read_current_weather_data(json_doc.object())
st = ' {\n' json_document_object = None if isinstance(json_document, QJsonDocument): json_document_object = json_document.object() elif isinstance(json_document, QJsonValue): json_document_object = json_document.toObject() for obj in json_document_object: st += obj + ': ' + get_type(json_document_object[obj]) + '\n' st = st[:-1] st += '\n}' return st return '' if __name__ == '__main__': parse_err = QJsonParseError() byte_array = read_js('example.json') json_document = QJsonDocument.fromJson(byte_array, parse_err) if QJsonParseError.NoError == parse_err.error: print(parse_err.errorString()) # if json_document.isObject(): # json_object = json_document.object() # for k in json_object: # print(k + ': ' + str(json_object[k].toVariant())) print(get_type(json_document))