Python - QStandardItem - TreeView

405 views Asked by At

I use qt with Python.

I want to implement a treeview with multiple columns. One column is already working.

If I add only one text item (variable txt) to the treeView it works)


class TreeStandardItem(QStandardItem):
    def __init__(self, txt='', font_size=12, set_bold=False, color=QColor(0, 0, 0)):
        super().__init__()

        fnt = QFont('Open Sans', font_size)
        fnt.setBold(set_bold)

        self.setEditable(False)
        self.setForeground(color)
        self.setFont(fnt)
        
        # one Item is added to treeView
        self.setText(txt) 


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
       #...
       #...
       texas = TreeStandardItem('texas', 10)
       nyc = TreeStandardItem('NYC', 10)

       # Create Link between parent and child
       texas.appendRow(nyc)

       treeView = self.ui.treeView1

       # Create Model
       treeModel = QStandardItemModel()
       rootNode = treeModel.invisibleRootItem()

       # Header
       treeModel.setHeaderData(0, Qt.Horizontal, 'Column-1')
       treeModel.setHeaderData(1, Qt.Horizontal, 'Column-2')
       treeModel.setHeaderData(2, Qt.Horizontal, 'Column-3')

        # Add roots to  the view
        rootNode.appendRow(texas)
   
        #...  

I want to add some Database Date for a hierarchical structure to the treeView- like:

Column1-------------------------------Column2------------------------Column3

-> Parent                            Firstname Lastname             User-ID
    -> Child 01                      Firstname Lastname             User-ID
       -> Another Child 01           Firstname Lastname             User-ID
       -> Another Child 02           Firstname Lastname             User-ID
    -> Child 02                      Firstname Lastname             User-ID

Any solutions? If I changed the txt variable from string to list I get an error. Here is the altered code:

class StandardItem(QStandardItem):
    def __init__(self, txt=[], font_size=10, set_bold=False, color=QColor(0, 0, 0)):
        super().__init__()

        fnt = QFont('Open Sans', font_size)
        fnt.setBold(set_bold)

        self.setEditable(False)
        self.setForeground(color)
        self.setFont(fnt)

        # works - iterating with list item is ok
        # self.setText(txt[1]) 

        # --------------- !!!! ---------------
        # Need something to loop over the columns..
        # --------------- !!!! ---------------



class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        # .....

        # Create model 
        
        treeModel = QStandardItemModel()
        rootNode = treeModel.invisibleRootItem()

        parent01 = StandardItem(['108112', 'Mary Luke', '55' ], 12, set_bold=True)
        rootNode.appendRow(parent01)

        child01 = StandardItem(['108108', 'Lukas Andrews', '13' ], 12, set_bold=True)
        parent01.appendRow(child01)

0

There are 0 answers