Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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



  • @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!


  • Lifetime Qt Champion

    Hi,

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



  • @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



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

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

    Does that change the behaviour?



  • @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



  • @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?


  • Lifetime Qt Champion

    I was also thinking about QModelIndex.data.



  • @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!



  • @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.



  • @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?



  • @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()
    


  • @JonB Thanks...I believe I got it


Log in to reply