Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to read/update widget(s) in QTreeView

How to read/update widget(s) in QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 958 Views
  • 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.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by ShinSat
    #1

    Hello,

    I'm trying to implement a program that reads and updates a row in QTreeView using indexWidget, but no luck so far. (indexWidget always returns None... :<
    Would you correct me to a right direction?
    My current test code is as follows.

    import sys, os, pprint, time
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    class App(QWidget):
        def __init__(self):
            super().__init__()
    
            self.vbox = QVBoxLayout()
            self.view = QTreeView()
            self.view.setSelectionBehavior(QAbstractItemView.SelectRows)
    
            self.model = QStandardItemModel()
            self.model.setHorizontalHeaderLabels(['col1', 'col2', 'col3'])
            self.view.setModel(self.model)
            self.view.setUniformRowHeights(True)
    
            self.vbox.addWidget(self.view)
            self.setLayout(self.vbox)
    
            for i in range(3):
                parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
                for j in range(3):
                    child1 = QStandardItem('Child {}'.format(i*3+j))
                    child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
                    child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
                    parent1.appendRow([child1, child2, child3])
                self.model.appendRow(parent1)
                # span container columns
                self.view.setFirstColumnSpanned(i, self.view.rootIndex(), True)
    
            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            # expand third container
            index = self.model.indexFromItem(parent1)
            self.view.expand(index)
            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            # select last row
            selmod = self.view.selectionModel()
            index2 = self.model.indexFromItem(child3)
            selmod.select(index2, QItemSelectionModel.Select|QItemSelectionModel.Rows)
            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            #view.show()
            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
        def get_data_row(self, row):
            row_wgt = self.view.indexWidget(self.model.createIndex(1,1))
            self.model.appendRow(row_wgt)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        wgt = App()
        wgt.show()
    
        wgt.get_data_row(0)
    
        sys.exit(app.exec_())
    
    

    Many many thanks in advance for your help.
    Regards,
    Sat

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

      Hi
      The data is in the model. Not the view.
      So you would use its Data function
      http://doc.qt.io/qt-5/qabstractitemmodel.html#data
      to read and insert new data to the model with
      insertRow

      S 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        The data is in the model. Not the view.
        So you would use its Data function
        http://doc.qt.io/qt-5/qabstractitemmodel.html#data
        to read and insert new data to the model with
        insertRow

        S Offline
        S Offline
        ShinSat
        wrote on last edited by
        #3

        @mrjj It works! Thanks for your help!!
        I'll continue to test further to reach to my goal. :)
        BTW, I'm still not sure if dataChanged signal is required to emit to update/insert the model?

        Regards,
        Sat

        mrjjM 1 Reply Last reply
        0
        • S ShinSat

          @mrjj It works! Thanks for your help!!
          I'll continue to test further to reach to my goal. :)
          BTW, I'm still not sure if dataChanged signal is required to emit to update/insert the model?

          Regards,
          Sat

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @ShinSat said in How to read/update widget(s) in QTreeView:

          dataChanged

          hi
          Its normal to emit that to tell the view(s) that model/data has changed.
          But if you didnt make you own setData() , it might do it automatically.

          S 1 Reply Last reply
          2
          • mrjjM mrjj

            @ShinSat said in How to read/update widget(s) in QTreeView:

            dataChanged

            hi
            Its normal to emit that to tell the view(s) that model/data has changed.
            But if you didnt make you own setData() , it might do it automatically.

            S Offline
            S Offline
            ShinSat
            wrote on last edited by
            #5

            Thanks a lot, @mrjj
            I'll carefully test, and then, go up to next level. :)

            Regards,
            Sat

            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