Search code examples
pythonpython-3.xpyside2

How to add Column Items to QtStandardItem


I'm trying to add Items to a Row, but appendColum is giving

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

I can't get a clue to what I'm doing wrong.

QList doesn't exist in Python, and I already tried others, like pythonlists, QListWidgets (as examples), and coudn't find/understand what typing.Sequence[PySide2.QtGui.QStandardItem] means

In the following example code, when "testROWONLY" is False, I get the error., otherwise it just add rows in the hierarchy, which is not the goal.

The goal is to add the JSONDICTs as columns of the key ID on whatever level there is ..,

This part of the JSON example test string shows a DICT : "Id" , with an array ., they should be columns on that row.,

{ "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_())

Solution

  • typing.Sequence[PySide2.QtGui.QStandardItem]) means a list of QStandardItems.

    In your last else you're adding a simple string, which clearly is not a correct argument type.
    Change that to:

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