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. QtreeView only able to sort by first and second column. Ia want to be able to sort by any column
Forum Updated to NodeBB v4.3 + New Features

QtreeView only able to sort by first and second column. Ia want to be able to sort by any column

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 3 Posters 528 Views 1 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.
  • B Offline
    B Offline
    babar
    wrote on last edited by
    #1

    I have a Qtreeview as follow in the image:

    54dd1ac0-9f8e-4c70-97aa-d6031c88fed7-image.png

    it is filled from nested dictionnaries such as:

    {well:{depthtype:{logtype:[log1,log2,log3]}}} all key and list values are strings

    I can sort by well name, by depth type, but not by log type or log name

    the model is a QstandardItemModel

    I add the data in the model with the following function:

    def fillWellTree(self, parent, dico, depth=0):
            if isinstance(dico, dict):
                for key, value in dico.items():
                    item1=QStandardItem(str(key))
                    item1.setEditable(False)
                    itemList=[item1]
                    if depth==0:
                        itemList[0].setIcon(QIcon("./icon/IconWell.png"))
                    if isinstance(value, dict):
                        for i in range(depth):
                            emptyItem=QStandardItem("")
                            emptyItem.setEditable(False)
                            itemList.insert(0,emptyItem)
                        parent.appendRow(itemList)
                        self.fillWellTree(itemList[0], value, depth+1)
                    elif isinstance(value, list):
                        for i in range(depth):
                            emptyItem=QStandardItem("")
                            emptyItem.setEditable(False)
                            itemList.insert(0,emptyItem)
                        parent.appendRow(itemList)
                        for val in value:
                            item_i=QStandardItem(str(val))
                            item_i.setEditable(False)
                            itemLogList=[item_i]
                            for i in range(depth+1):
                                emptyItem=QStandardItem("")
                                emptyItem.setEditable(False)
                                itemLogList.insert(0,emptyItem)
                            itemList[0].appendRow(itemLogList)
    

    I do not quite understand why only the 2 first columns are sortable.

    Thank you for your help

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which version of PyQt / PySide are you using ?
      On which OS ?
      Can you provide a minimal runnable script that shows this behavior ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • B Offline
        B Offline
        babar
        wrote on last edited by
        #3

        Hi
        Thank you. I am using PySide6 on windows
        here is a runnable example that demonstrate the problem

        import sys
        
        from PySide6.QtWidgets import QApplication, QTreeView
        from PySide6.QtGui import QStandardItem, QStandardItemModel
        
        app = QApplication(sys.argv)
        
        TreeViewLogSelection=QTreeView()
        WellModel = QStandardItemModel()
        WellModel.setColumnCount(6)
        TreeViewLogSelection.setModel(WellModel)
        TreeViewLogSelection.model().setHorizontalHeaderLabels(['Well Name','Depth type','Log Type','Log Name','Role','Log unique name'])  
         
        def fillWellTree(parent, dico, depth=0):
            if isinstance(dico, dict):
                for key, value in dico.items():
                    item1=QStandardItem(str(key))
                    item1.setEditable(False)
                    itemList=[item1]
                    if isinstance(value, dict):
                        for i in range(depth):
                            emptyItem=QStandardItem("")
                            emptyItem.setEditable(False)
                            itemList.insert(0,emptyItem)
                        parent.appendRow(itemList)
                        fillWellTree(itemList[0], value, depth+1)
                    elif isinstance(value, list):
                        for i in range(depth):
                            emptyItem=QStandardItem("")
                            emptyItem.setEditable(False)
                            itemList.insert(0,emptyItem)
                        parent.appendRow(itemList)
                        for val in value:
                            item_i=QStandardItem(str(val))
                            item_i.setEditable(False)
                            itemLogList=[item_i]
                            for i in range(depth+1):
                                emptyItem=QStandardItem("")
                                emptyItem.setEditable(False)
                                itemLogList.insert(0,emptyItem)
                            itemList[0].appendRow(itemLogList)   
            TreeViewLogSelection.setSortingEnabled(True)             
        dico={'Well-6': {'Depth': {'Sonic': ['DT', 'DTS', 'DTST', 'VPVS', 'DT_REG', 'Smoothing (DT_REG)', 'DT_FINAL'], 'Shallow resistivity': ['LLS', 'MSFL'], 'Bulk density': ['RHOB_RAW', 'RHOB_Predict_RFA', 'RHOB_REG', 'RHOB_DESPIKED', 'RHOB_DESPIKE_REG', 'Smoothing (RHOB_DESPIKE_REG)', 'RHOB_FINAL', 'RHOB_Predict_RF'], 'Shear slowness': ['DTS_Predict_RF', 'DTS_Predict_NN'], 'Deviation': ['DX', 'DY']}, 'MD': {'Sonic': ['DT_Predict_RF','DTS_predict']}}, 'DRILL-1': {'Depth': {'Bit size': ['BS'], 'Caliper': ['CALI'], 'Gamma ray': ['GR'], 'Neutron': ['NPHI'], 'Photoelectric factor': ['PE'], 'Spontaneous potential': ['SP'], 'Shallow resistivity': ['LLS'], 'Deep resistivity': ['LLD'], 'Sonic': ['DT', 'DTS','DTC'], 'Bulk density': ['RHOB'], 'Anonymous': ['WELLTOPS'], 'Badhole indicator': ['Bad_Hole']}}, 'WELLI-1': {'Depth': {'Sonic': ['DT', 'DTS','DTC'], 'Gamma ray': ['GR', 'GR2'], 'Shallow resistivity': ['LLS', 'MSFL'], 'Bulk density': ['RHOB_RAW', 'RHOB_Predict_RF']}, 'MD': {'Sonic': ['DT_Predict_RF', 'DT_Merged', 'DT_FINAL'], 'Impedance': ['AI','IP']}}}
        fillWellTree(WellModel.invisibleRootItem(),dico)
        TreeViewLogSelection.show()
        sys.exit(app.exec())
        

        Thank you for your help

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Which version of PyQt / PySide are you using ?
          On which OS ?
          Can you provide a minimal runnable script that shows this behavior ?

          B Offline
          B Offline
          babar
          wrote on last edited by babar
          #4

          @SGaist Hi thank you very much for your help
          I just added a running code in the thread (just forgot to answer directly to you)
          I am using Pyside6 on windows

          JonBJ 1 Reply Last reply
          0
          • B babar

            @SGaist Hi thank you very much for your help
            I just added a running code in the thread (just forgot to answer directly to you)
            I am using Pyside6 on windows

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @babar said in QtreeView only able to sort by first and second column. Ia want to be able to sort by any column:

            I am using Pyside6 on windows

            Let him know the exact version of PySide6/Qt6 you are using, i.e. 6.x what "x"?

            B 1 Reply Last reply
            0
            • JonBJ JonB

              @babar said in QtreeView only able to sort by first and second column. Ia want to be able to sort by any column:

              I am using Pyside6 on windows

              Let him know the exact version of PySide6/Qt6 you are using, i.e. 6.x what "x"?

              B Offline
              B Offline
              babar
              wrote on last edited by
              #6

              @JonB @SGaist
              You're right sorry. I use version Version: 6.7.2

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I currently don't know why it does not work for these columns but in between, what you can do is use a QSortFilterProxyModel.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  babar
                  wrote on last edited by
                  #8

                  I got my answer : this issue is due to the way QStandardItemModel sorts the column, it only works if the parent item has valid item in the same column. The way I built the tree does not provide an item for all the columns of each appended row so I ended up with this issue.
                  solution1 create these empty items
                  solution 2 use a QSortFilterProxyModel
                  Anyway a big thank you for your support attention and help.

                  1 Reply Last reply
                  1

                  • Login

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