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. Trying to get the id of an item when i click it in a QTreeView
Forum Updated to NodeBB v4.3 + New Features

Trying to get the id of an item when i click it in a QTreeView

Scheduled Pinned Locked Moved Solved Qt for Python
12 Posts 3 Posters 2.8k 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.
  • A Offline
    A Offline
    adrian88888888
    wrote on last edited by
    #1

    Hi again!
    I keep trying and trying to get the id of an item when i click it in a QTreeView

    and i finally managed to do it!:
    alt text
    but then i realized that if I change the order of the items in the view no longer gives me the correct id:
    alt text
    here's the code:

    self.ui.mainTree.clicked.connect(self.getID)
    
    def getID(self, val):
            row = val.row()
            column = val.column()
            parent = val.parent()
            index = self.ui.model.index(row, 0, parent)
            value = self.ui.model.data(index, Qt.DisplayRole)
            print('id:', value)
    

    any suggestions are very very appreciated

    btw, thanks a lot for all the help in this forum, i can't stress it enough

    1 Reply Last reply
    0
    • JonBJ JonB

      @adrian88888888
      I was only clarifying what @SGaist had said, I was unsure how that would affect the behaviour, but he usually knows best :)

      It seems to me you are wanting to map between the QTreeView index and that of the model because of sorting? Your self.ui.model is what? There isn't a QProxyModel here is there? And your self.ui.mainTree is connected to this self.ui.model? And your title says QTreeView, self.ui.mainTree is that and not a QTreeWidget?

      A Offline
      A Offline
      adrian88888888
      wrote on last edited by
      #8

      @JonB This answer questions:

          def Starting_DataBase(self):
              #CONNECTING TO THE DATABASE:
              self.ui.db = QSqlDatabase.addDatabase('QSQLITE')
              self.ui.db.setDatabaseName('Database_Dir/Database_Dir/database.db')
              self.ui.db.open()
      
              #MAKING THE MODEL USING THE DATABASE:
              self.ui.model = QSqlTableModel()
              self.ui.model.setTable('Demos')
              self.ui.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
              self.ui.model.select()
      
              #PUTTING THE MODEL INSIDE A WARPER TO SORT AND FILTER IT:
              self.ui.model_sorted = QSortFilterProxyModel()
              self.ui.model_sorted.setSourceModel(self.ui.model)
      
              #MAKING THE RELATIONSHIP BETWEEN THE TABLEVIEW AND THE MODEL_SORTED:
              self.ui.mainTree.setModel(self.ui.model_sorted)
      

      omg im blind.... just realized....it should be self.ui.model_sorted instead of self.ui.model:

      def getID(self, val):
              index = val.sibling(val.row(), 0)
              value = self.ui.model_sorted.data(index, Qt.DisplayRole)
              print('id:', value)
      

      my problem is that i expect the mistakes to be in me not understanding how QT works and not in me doing wrong my code, because 99% of the mistakes were in me not understanding how QT works

      that's why i didn't saw it

      thanks thanks thsnk tasnk thnkt ths!

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

        Hi,

        You do not need to go through the model and view again. You can use QModelIndex::sibling to retrieve the data you want.

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

        A 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You do not need to go through the model and view again. You can use QModelIndex::sibling to retrieve the data you want.

          A Offline
          A Offline
          adrian88888888
          wrote on last edited by adrian88888888
          #3

          @SGaist Can you explain more what you mean by this:"You do not need to go through the model and view again", i'm doing that? where?, i barely understand my own code

          JonBJ 1 Reply Last reply
          0
          • A adrian88888888

            @SGaist Can you explain more what you mean by this:"You do not need to go through the model and view again", i'm doing that? where?, i barely understand my own code

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

            @adrian88888888
            @SGaist is suggesting: Try setting index just via in:

            index = val.sibling(val.row(), 0)
            

            Does that change the behaviour?

            A 1 Reply Last reply
            1
            • JonBJ JonB

              @adrian88888888
              @SGaist is suggesting: Try setting index just via in:

              index = val.sibling(val.row(), 0)
              

              Does that change the behaviour?

              A Offline
              A Offline
              adrian88888888
              wrote on last edited by
              #5

              @JonB I did this:

              def getID(self, val):
                      index = val.sibling(val.row(), 0)
                      value = self.ui.model.data(index, Qt.DisplayRole)
                      print('id:', value)
              

              the behaviour it's exactly the same

              JonBJ 1 Reply Last reply
              0
              • A adrian88888888

                @JonB I did this:

                def getID(self, val):
                        index = val.sibling(val.row(), 0)
                        value = self.ui.model.data(index, Qt.DisplayRole)
                        print('id:', value)
                

                the behaviour it's exactly the same

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

                @adrian88888888
                I was only clarifying what @SGaist had said, I was unsure how that would affect the behaviour, but he usually knows best :)

                It seems to me you are wanting to map between the QTreeView index and that of the model because of sorting? Your self.ui.model is what? There isn't a QProxyModel here is there? And your self.ui.mainTree is connected to this self.ui.model? And your title says QTreeView, self.ui.mainTree is that and not a QTreeWidget?

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

                  I was also thinking about QModelIndex.data.

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

                  JonBJ 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @adrian88888888
                    I was only clarifying what @SGaist had said, I was unsure how that would affect the behaviour, but he usually knows best :)

                    It seems to me you are wanting to map between the QTreeView index and that of the model because of sorting? Your self.ui.model is what? There isn't a QProxyModel here is there? And your self.ui.mainTree is connected to this self.ui.model? And your title says QTreeView, self.ui.mainTree is that and not a QTreeWidget?

                    A Offline
                    A Offline
                    adrian88888888
                    wrote on last edited by
                    #8

                    @JonB This answer questions:

                        def Starting_DataBase(self):
                            #CONNECTING TO THE DATABASE:
                            self.ui.db = QSqlDatabase.addDatabase('QSQLITE')
                            self.ui.db.setDatabaseName('Database_Dir/Database_Dir/database.db')
                            self.ui.db.open()
                    
                            #MAKING THE MODEL USING THE DATABASE:
                            self.ui.model = QSqlTableModel()
                            self.ui.model.setTable('Demos')
                            self.ui.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
                            self.ui.model.select()
                    
                            #PUTTING THE MODEL INSIDE A WARPER TO SORT AND FILTER IT:
                            self.ui.model_sorted = QSortFilterProxyModel()
                            self.ui.model_sorted.setSourceModel(self.ui.model)
                    
                            #MAKING THE RELATIONSHIP BETWEEN THE TABLEVIEW AND THE MODEL_SORTED:
                            self.ui.mainTree.setModel(self.ui.model_sorted)
                    

                    omg im blind.... just realized....it should be self.ui.model_sorted instead of self.ui.model:

                    def getID(self, val):
                            index = val.sibling(val.row(), 0)
                            value = self.ui.model_sorted.data(index, Qt.DisplayRole)
                            print('id:', value)
                    

                    my problem is that i expect the mistakes to be in me not understanding how QT works and not in me doing wrong my code, because 99% of the mistakes were in me not understanding how QT works

                    that's why i didn't saw it

                    thanks thanks thsnk tasnk thnkt ths!

                    1 Reply Last reply
                    1
                    • SGaistS SGaist

                      I was also thinking about QModelIndex.data.

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

                      @SGaist said in Trying to get the id of an item when i click it in a QTreeView:

                      I was also thinking about QModelIndex.data.

                      Sneaky :)

                      @adrian88888888
                      Although you absolutely needed to "sort" this out :) Your self.ui.model.index() was producing the wrong index because it was against the wrong model. @SGaist was pointing out that using instead QModelIndex.data(), i.e. your val.data(), would ensure you were accessing the data in the model referred to by the index, and so would have worked.

                      A 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @SGaist said in Trying to get the id of an item when i click it in a QTreeView:

                        I was also thinking about QModelIndex.data.

                        Sneaky :)

                        @adrian88888888
                        Although you absolutely needed to "sort" this out :) Your self.ui.model.index() was producing the wrong index because it was against the wrong model. @SGaist was pointing out that using instead QModelIndex.data(), i.e. your val.data(), would ensure you were accessing the data in the model referred to by the index, and so would have worked.

                        A Offline
                        A Offline
                        adrian88888888
                        wrote on last edited by
                        #10

                        @JonB I got 3 def's by trying to understand what you say, and they all work, but they are all different:

                        def getID(self, val):
                                index = self.ui.model_sorted.index(val.row(), 0, val.parent())
                                value = self.ui.model_sorted.data(index, Qt.DisplayRole)
                                print('id:', value)
                        
                        def getID(self, val):
                                index = val.sibling(val.row(), 0)
                                value = self.ui.model_sorted.data(index, Qt.DisplayRole)
                                print('id:', value)
                        
                        def getID(self, val):
                                index = self.ui.model_sorted.index(val.row(), 0, val.parent())
                                value = QModelIndex.data(index, Qt.DisplayRole)
                                print('id:', value)
                        

                        If I understand what you say the 3rd def would be the correct one, right?

                        JonBJ 1 Reply Last reply
                        0
                        • A adrian88888888

                          @JonB I got 3 def's by trying to understand what you say, and they all work, but they are all different:

                          def getID(self, val):
                                  index = self.ui.model_sorted.index(val.row(), 0, val.parent())
                                  value = self.ui.model_sorted.data(index, Qt.DisplayRole)
                                  print('id:', value)
                          
                          def getID(self, val):
                                  index = val.sibling(val.row(), 0)
                                  value = self.ui.model_sorted.data(index, Qt.DisplayRole)
                                  print('id:', value)
                          
                          def getID(self, val):
                                  index = self.ui.model_sorted.index(val.row(), 0, val.parent())
                                  value = QModelIndex.data(index, Qt.DisplayRole)
                                  print('id:', value)
                          

                          If I understand what you say the 3rd def would be the correct one, right?

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

                          @adrian88888888
                          No, it's 2 + 3 merged. We are trying never to reference self.ui.model-anything here, as we're saying that was wrong. We'd like to do everything off the index parameter only.

                              index = val.sibling(val.row(), 0)     # just go via index sibling()
                              value = index.data(Qt.DisplayRole)    # just via via index data()
                          
                          A 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @adrian88888888
                            No, it's 2 + 3 merged. We are trying never to reference self.ui.model-anything here, as we're saying that was wrong. We'd like to do everything off the index parameter only.

                                index = val.sibling(val.row(), 0)     # just go via index sibling()
                                value = index.data(Qt.DisplayRole)    # just via via index data()
                            
                            A Offline
                            A Offline
                            adrian88888888
                            wrote on last edited by
                            #12

                            @JonB Thanks...I believe I got it

                            1 Reply Last reply
                            0

                            • Login

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