如何将列项添加到 QStandardItem

How to add Column Items to QtStandardItem

我正在尝试将项目添加到行中,但 appendColum 正在提供

parent.appendColumn( sbl )
TypeError: 'PySide2.QtGui.QStandardItem.appendColumn' called with wrong argument types:
  PySide2.QtGui.QStandardItem.appendColumn(list)
Supported signatures:
  PySide2.QtGui.QStandardItem.appendColumn(typing.Sequence[PySide2.QtGui.QStandardItem])
11:16:05: /usr/bin/python3 exited with code 1

我不知道自己做错了什么。

QList 在 Python 中不存在,我已经尝试过其他的,比如 pythonlists、QListWidgets(作为示例),find/understand 什么 typing.Sequence[PySide2.QtGui.QStandardItem] 表示

在下面的示例代码中,当“testROWONLY”为False时, 我得到了错误。,否则它只是在层次结构中添加行,这不是目标。

目标是将 JSONDICT 作为密钥 ID 的列添加到任何级别上..,

JSON 示例测试字符串的这一部分显示了一个 DICT:“Id”,带有一个数组。它们应该是该行上的列。,

{ "00000157438584" : [ { "Id" : [  4136 , "J" ] }, "NULL", "Uma Empresa Mestre MEI", 22181, { "dtInt" : [ 3 , 43908 ] }, "00000157438584" ] },
# This Python file uses the following encoding: utf-8

import json

from PySide2 import *
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtUiTools import *


testROWONLY = False


def fill_model_from_json(parent, d, row=True):
    if isinstance(d, dict):
        print("Dict :" , d )
        for k, v in d.items():
            child = QStandardItem(str(k))
            parent.appendRow(child)
            fill_model_from_json(child, v)
    elif isinstance(d, list):
        print("List :" , d )
        for v in d:
            if testROWONLY:
                fill_model_from_json(parent, v)
            else:
                sbl = []
                if isinstance( v , dict ) or isinstance( v, list ):
                    print("Internal List or dict as item" )
                    child = QStandardItem(str(v))
                    fill_model_from_json(child, v)
                    sbl.append(child)
                else:
                    print("Item de Coluna" )
                    sbl.append( str(v) )
                parent.appendColumn( sbl )
        #
    else:
        print("Row:", row, ", Valor :" , d )
        parent.appendRow(QtGui.QStandardItem(str(d)))

def fill_dict_from_model(parent_index, d):
    v = {}
    for i in range(model.rowCount(parent_index)):
        ix = model.index(i, 0, parent_index)
        fill_dict_from_model(ix, v)
    d[parent_index.data()] = v

def model_to_dict(model):
    d = dict()
    for i in range(model.rowCount()):
        ix = model.index(i, 0)
        fill_dict_from_model(ix, d)
    return d


testJSONString = """[
{ "00000157438584" : [ { "Id" : [  4136 , "J" ] }, "NULL", "Uma Empresa Mestre MEI", 22181, { "dtInt" : [ 3 , 43908 ] }, "00000157438584" ] },
{ "00000342877779" : [ { "Id" : [ 18845 , "H" ] }, "NULL", "Outra Empresa Mestre EIRELI", 26691, { "dtInt" : [ 2 , 46351 ] }, "00000342877779" ] },
{ "00000571608823" : [ { "Id" : [ 20851 , "G" ] }, "NULL", "Mais Uma Empresa Total MEI", 31508, { "dtInt" : [ 5 , 61162 ] }, "00000571608823" ] },
{ "00000764215701" : [ { "Id" : [ 17852 , "H" ] }, "NULL", "Ainda Empresa Total EIRELI", 33922, { "dtInt" : [ 3 , 59695 ] }, "00000764215701" ] },
{ "00000966091844" : [ { "Id" : [ 16340 , "K" ] }, "NULL", "Uma Companhia Mestre MEI", 34485, { "dtInt" : [ 2 , 59583 ] }, "00000966091844" ] },
{ "00001040080606" : [ { "Id" : [ 15585 , "J" ] }, "NULL", "Outra Companhia Mestre EIRELI", 35530, { "dtInt" : [ 6 , 58528 ] }, "00001040080606" ] },
{ "00001180903195" : [ { "Id" : [ 13773 , "P" ] }, "NULL", "Mais uma Companhia Total MEI", 37256, { "dtInt" : [ 2 , 55107 ] }, "00001180903195" ] },
{ "00001271948415" : [ { "Id" : [ 12691 , "F" ] }, "NULL", "Mais Outras Companhia Total EIRELI", 37954, { "dtInt" : [ 2 , 54860 ] }, "00001271948415" ] },
{ "00001951304915" : [ { "Id" : [ 12090 , "G" ] }, "NULL", "Um Verdurao Mestre MEI", 39732, { "dtInt" : [ 1 , 54020 ] }, "00001951304915" ] },
{ "00001953546566" : [ { "Id" : [ 11344 , "G" ] }, "NULL", "Outro Verdurao Mestre EIRELI", 43292, { "dtInt" : [ 1 , 51624 ] }, "00001953546566" ] },
{ "00001994748455" : [ { "Id" : [  8745 , "T" ] }, "NULL", "Mais Um Verdurao Total MEI", 21795, { "dtInt" : [ 7 , 49724 ] }, "00001994748455" ] },
{ "00002119061407" : [ { "Id" : [  4665 , "F" ] }, "NULL", "Mais Outro Verdurao Total EIRELI", 20181, { "dtInt" : [ 9 , 49309 ] }, "00002119061407" ] },
{ "00002139990302" : [ { "Id" : [  4538 , "K" ] }, "NULL", "Ultimo Emporio Mestre MEI", 20051, { "dtInt" : [ 1 , 46538 ] }, "00002139990302" ] }
]
"""




if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    tree = QTreeView()
    model = QStandardItemModel()
    #
    #
    js = json.loads( testJSONString )
    print( js )
    #  data =  {"A": {"B": {"H": {}, "I": {"M": {}, "N": {}}}, "D": {}, "E": {}, "F": {}, "G": {"L": {}}, "C": {"J": {}, "K": {}}}}

    fill_model_from_json(model.invisibleRootItem(), js )
    tree.setModel(model)
    tree.expandAll()
    tree.resize(360, 480)
    tree.show()
    # d = model_to_dict(model)
    # assert(d == data)
    # print(d)
    sys.exit(app.exec_())

typing.Sequence[PySide2.QtGui.QStandardItem]) 表示 QStandardItems 的列表。

在上一个 else 中,您添加了一个简单的字符串,这显然不是正确的参数类型。
将其更改为:

    else:
        print("Item de Coluna" )
        sbl.append(QStandardItem(str(v)))