Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. modified Qt example editabletreemodel
Forum Updated to NodeBB v4.3 + New Features

modified Qt example editabletreemodel

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 2 Posters 352 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MasterQM Offline
    MasterQM Offline
    MasterQ
    wrote on last edited by MasterQ
    #1

    Hello,

    I am stuck with QTreeView. I don't get it how it works.

    Following that offical example for the implementation of a QTreeView (Editable Tree Model Example) I have slightly modified it.

    The only change is in the constructor of TreeModel. I commented out the read of the file default.txt and add some items by hand

    Window:

            self.model = TreeModel(["eins" ,"zwei"], "drei")
            self.ui.treeView.setModel(self.model)
    

    TreeModel:

    class TreeModel(QAbstractItemModel):
    
        def __init__(self, headers: list, data: str, parent=None):
            super().__init__(parent)
    
            self.root_data = headers
            self.root_item = TreeItem(self.root_data.copy())
            # self.setup_model_data(data.split("\n"), self.root_item)
    
            i1 = TreeItem(["1", "2"])
    
            i11 = TreeItem(["11", "12"])
            i1.child_items.insert(0, i11)
    
            i12 = TreeItem(["21", "22"])
            i1.child_items.insert(1, i12)
    
            self.root_item.child_items.insert(0, i1)
    
            qDebug(self.__repr__())
    

    Tree structure (i.e. qDebug(self.__repr__())):

    <treeitem.TreeItem at 0x7f458590b320 "eins" "zwei", 1 children>
      <treeitem.TreeItem at 0x7f458590b8f0 "1" "2", 2 children>
        <treeitem.TreeItem at 0x7f458590b920 "11" "12", 0 children>
        <treeitem.TreeItem at 0x7f458590b950 "21" "22", 0 children>
    

    View:

    c2f32660-7653-41ba-bf15-4c8f8753976f-grafik.png

    You can easily see, that the data tree is correct but the view is showing some weird result.

    What's wrong here? I do not find the mistake. And again, the original example is working and my small modification is braking it.

    J.

    Pl45m4P 1 Reply Last reply
    0
    • MasterQM Offline
      MasterQM Offline
      MasterQ
      wrote on last edited by
      #6

      Oh dear, it's Friday.

      the isue was that I called

              i11 = ProjectsTreeItem(["11", "12"])
      

      instead of

              i11 = ProjectsTreeItem(["11", "12"],i1)
      

      I've forgotten to give the parent parameter, which is set to None as default.

      1 Reply Last reply
      0
      • MasterQM MasterQ

        Hello,

        I am stuck with QTreeView. I don't get it how it works.

        Following that offical example for the implementation of a QTreeView (Editable Tree Model Example) I have slightly modified it.

        The only change is in the constructor of TreeModel. I commented out the read of the file default.txt and add some items by hand

        Window:

                self.model = TreeModel(["eins" ,"zwei"], "drei")
                self.ui.treeView.setModel(self.model)
        

        TreeModel:

        class TreeModel(QAbstractItemModel):
        
            def __init__(self, headers: list, data: str, parent=None):
                super().__init__(parent)
        
                self.root_data = headers
                self.root_item = TreeItem(self.root_data.copy())
                # self.setup_model_data(data.split("\n"), self.root_item)
        
                i1 = TreeItem(["1", "2"])
        
                i11 = TreeItem(["11", "12"])
                i1.child_items.insert(0, i11)
        
                i12 = TreeItem(["21", "22"])
                i1.child_items.insert(1, i12)
        
                self.root_item.child_items.insert(0, i1)
        
                qDebug(self.__repr__())
        

        Tree structure (i.e. qDebug(self.__repr__())):

        <treeitem.TreeItem at 0x7f458590b320 "eins" "zwei", 1 children>
          <treeitem.TreeItem at 0x7f458590b8f0 "1" "2", 2 children>
            <treeitem.TreeItem at 0x7f458590b920 "11" "12", 0 children>
            <treeitem.TreeItem at 0x7f458590b950 "21" "22", 0 children>
        

        View:

        c2f32660-7653-41ba-bf15-4c8f8753976f-grafik.png

        You can easily see, that the data tree is correct but the view is showing some weird result.

        What's wrong here? I do not find the mistake. And again, the original example is working and my small modification is braking it.

        J.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @MasterQ said in modified Qt example editabletreemodel:

        self.model = ProjectsTreeModel(["eins" ,"zwei"], "drei")

        Where is ProjectsTreeModel suddenly coming from?!
        Your implementation is called TreeModel, but you seem to create another model class.

        Also:
        ProjectsTreeModel(["eins" ,"zwei"], "drei") creates two columns with Header "Eins" und "Zwei" and you pass "drei" as data?!
        On purpose?!


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        MasterQM 1 Reply Last reply
        0
        • MasterQM Offline
          MasterQM Offline
          MasterQ
          wrote on last edited by
          #3

          sry that are typos. In my modified example it is not TreeModel but ProjectsTreeModel. I just forgot to rename. I just do correct it.

          Pl45m4P 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @MasterQ said in modified Qt example editabletreemodel:

            self.model = ProjectsTreeModel(["eins" ,"zwei"], "drei")

            Where is ProjectsTreeModel suddenly coming from?!
            Your implementation is called TreeModel, but you seem to create another model class.

            Also:
            ProjectsTreeModel(["eins" ,"zwei"], "drei") creates two columns with Header "Eins" und "Zwei" and you pass "drei" as data?!
            On purpose?!

            MasterQM Offline
            MasterQM Offline
            MasterQ
            wrote on last edited by
            #4

            @Pl45m4 said in modified Qt example editabletreemodel:

            ProjectsTreeModel(["eins" ,"zwei"], "drei") creates two columns with Header "Eins" und "Zwei" and you pass "drei" as data?!
            On purpose?!

            Yes, I just have to give data and this "drei" is a placeholder. It is not used in the model anyway.

            1 Reply Last reply
            0
            • MasterQM MasterQ

              sry that are typos. In my modified example it is not TreeModel but ProjectsTreeModel. I just forgot to rename. I just do correct it.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #5

              @MasterQ

              Haven't checked everything, but I think it has to do with the fact that you commented this line:

              # self.setup_model_data(data.split("\n"), self.root_item)
              

              Check the impl. of this function:

                  def setup_model_data(self, lines: list, parent: TreeItem):
                      parents = [parent]
                      indentations = [0]
              
                      for line in lines:
                          line = line.rstrip()
                          if line and "\t" in line:
              
                              position = 0
                              while position < len(line):
                                  if line[position] != " ":
                                      break
                                  position += 1
              
                              column_data = line[position:].split("\t")
                              column_data = [string for string in column_data if string]
              
                              if position > indentations[-1]:
                                  if parents[-1].child_count() > 0:
                                      parents.append(parents[-1].last_child())
                                      indentations.append(position)
                              else:
                                  while position < indentations[-1] and parents:
                                      parents.pop()
                                      indentations.pop()
              
                              parent: TreeItem = parents[-1]
                              col_count = self.root_item.column_count()
                              parent.insert_children(parent.child_count(), 1, col_count)
              
                              for column in range(len(column_data)):
                                  child = parent.last_child()
                                  child.set_data(column, column_data[column])
              

              It sets up the model/view to prepare/display the data properly.
              Just calling insertItem is not enough for a custom editable model implementation


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • MasterQM Offline
                MasterQM Offline
                MasterQ
                wrote on last edited by
                #6

                Oh dear, it's Friday.

                the isue was that I called

                        i11 = ProjectsTreeItem(["11", "12"])
                

                instead of

                        i11 = ProjectsTreeItem(["11", "12"],i1)
                

                I've forgotten to give the parent parameter, which is set to None as default.

                1 Reply Last reply
                0
                • MasterQM MasterQ has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved