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. insertRows and insertRow in tableview
QtWS25 Last Chance

insertRows and insertRow in tableview

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 3.2k 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.
  • B Offline
    B Offline
    blossomsg
    wrote on 6 Oct 2021, 17:23 last edited by
    #1

    Hey all,
    This is a continuation of my previous post
    https://forum.qt.io/topic/130364/headerdata-of-qabstracttablemodel-unable-to-return-qtcore-qvariant-list-of-strings/6

    expecting_a_5th_row.jpg

    I am trying to insertRows through insertRow -- expecting 5th row
    as per the docs
    but i guess i am doing something wrong, i know i am suppose to put rowCount somewhere in insertRows or insertRow

    and if you can elaborate difference between rowCount and insertRows or insertRow

    can you please guide me through

    from PyQt5 import QtCore
    
    
    class TableModel(QtCore.QAbstractTableModel):
    	def __init__(self, cols_strings, rows_strings, empty_strings, parent=None):
    		super(TableModel, self).__init__(parent)
    
    		self.colStringsList = cols_strings
    		self.rowStringsList = rows_strings
    		self.data = empty_strings
    
    	def rowCount(self, index=QtCore.QModelIndex()):
    		return len(self.data)
    
    	def columnCount(self, index=QtCore.QModelIndex()):
    		return len(self.data[0])
    
    	def data(self, index=QtCore.QModelIndex, role=QtCore.Qt.DisplayRole):
    		if index.isValid():
    			if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
    				value = self.data[index.row()][index.column()]
    				return str(value)
    
    	def setData(self, index=QtCore.QModelIndex, value=QtCore.QVariant, role=QtCore.Qt.EditRole):
    		if role == QtCore.Qt.EditRole:
    			self.data[index.row()][index.column()] = value
    			return True
    		return False
    
    	def headerData(self, section, orientation=QtCore.Qt.Orientation, role=QtCore.Qt.DisplayRole):
    		if role == QtCore.Qt.DisplayRole:
    			if orientation == QtCore.Qt.Horizontal:
    				return self.colStringsList[section]
    			if orientation == QtCore.Qt.Vertical:
    				return self.rowStringsList[section]
    
    	def flags(self, index):
    		return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable
    
    	def insertRow(self, position, index=QtCore.QModelIndex()):
    		self.insertRows(position, 1, index)
    
    	def insertRows(self, position, rows=1, index=QtCore.QModelIndex()):
    		self.beginInsertRows(index, position, position + rows - 1)
    		for row in range(0, rows):
    			self.data.insert(position, "")
    		self.endInsertRows()
    		return True
    
    
    J 1 Reply Last reply 6 Oct 2021, 20:45
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 6 Oct 2021, 18:23 last edited by
      #2

      I don't see what should be wrong with your code nor a real question in your post. So what do you do, expect and actually get?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 Oct 2021, 18:26 last edited by
        #3

        Hi,

        There's no need to reimplement insertRow as the base implementation calls insertRows anyway.

        What is exactly your issue ?

        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
        2
        • B Offline
          B Offline
          blossomsg
          wrote on 6 Oct 2021, 20:00 last edited by
          #4

          Apologies for not explaining well
          but when i run the above code(model) with ui the 5th row does not appear
          and with pycharm debugger no errors were prompted
          any suggestion would be really helpful

          1 Reply Last reply
          0
          • B blossomsg
            6 Oct 2021, 17:23

            Hey all,
            This is a continuation of my previous post
            https://forum.qt.io/topic/130364/headerdata-of-qabstracttablemodel-unable-to-return-qtcore-qvariant-list-of-strings/6

            expecting_a_5th_row.jpg

            I am trying to insertRows through insertRow -- expecting 5th row
            as per the docs
            but i guess i am doing something wrong, i know i am suppose to put rowCount somewhere in insertRows or insertRow

            and if you can elaborate difference between rowCount and insertRows or insertRow

            can you please guide me through

            from PyQt5 import QtCore
            
            
            class TableModel(QtCore.QAbstractTableModel):
            	def __init__(self, cols_strings, rows_strings, empty_strings, parent=None):
            		super(TableModel, self).__init__(parent)
            
            		self.colStringsList = cols_strings
            		self.rowStringsList = rows_strings
            		self.data = empty_strings
            
            	def rowCount(self, index=QtCore.QModelIndex()):
            		return len(self.data)
            
            	def columnCount(self, index=QtCore.QModelIndex()):
            		return len(self.data[0])
            
            	def data(self, index=QtCore.QModelIndex, role=QtCore.Qt.DisplayRole):
            		if index.isValid():
            			if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            				value = self.data[index.row()][index.column()]
            				return str(value)
            
            	def setData(self, index=QtCore.QModelIndex, value=QtCore.QVariant, role=QtCore.Qt.EditRole):
            		if role == QtCore.Qt.EditRole:
            			self.data[index.row()][index.column()] = value
            			return True
            		return False
            
            	def headerData(self, section, orientation=QtCore.Qt.Orientation, role=QtCore.Qt.DisplayRole):
            		if role == QtCore.Qt.DisplayRole:
            			if orientation == QtCore.Qt.Horizontal:
            				return self.colStringsList[section]
            			if orientation == QtCore.Qt.Vertical:
            				return self.rowStringsList[section]
            
            	def flags(self, index):
            		return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable
            
            	def insertRow(self, position, index=QtCore.QModelIndex()):
            		self.insertRows(position, 1, index)
            
            	def insertRows(self, position, rows=1, index=QtCore.QModelIndex()):
            		self.beginInsertRows(index, position, position + rows - 1)
            		for row in range(0, rows):
            			self.data.insert(position, "")
            		self.endInsertRows()
            		return True
            
            
            J Offline
            J Offline
            jeremy_k
            wrote on 6 Oct 2021, 20:45 last edited by
            #5

            @blossomsg said in insertRows and insertRow in tableview:

            	def data(self, index=QtCore.QModelIndex, role=QtCore.Qt.DisplayRole):
            		if index.isValid():
            			if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            				value = self.data[index.row()][index.column()]
            				return str(value)
            
            	def insertRows(self, position, rows=1, index=QtCore.QModelIndex()):
            		self.beginInsertRows(index, position, position + rows - 1)
            		for row in range(0, rows):
            			self.data.insert(position, "")
            		self.endInsertRows()
            		return True
            
            

            TableModel.insertRows() is inserting an empty string into data. TableModel.data() attempts to index into that string, which will fail.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            1
            • B Offline
              B Offline
              blossomsg
              wrote on 7 Oct 2021, 09:40 last edited by
              #6

              Hey @jeremy_k
              thanks for reply.
              in the ui class i am passing list of empty strings, as i don't want it prefilled
              i assumed model will be enough to explain the situation
              below is the ui code
              i am looking for how to get n-number of rows with insertRows, but no success

              from ui.table_view_delgate import Ui_Form
              from tablemodel import TableModel
              from PyQt5 import QtWidgets
              from PyQt5 import QtGui
              import sys
              import json
              
              with open('configuration_file.json') as data:
              	config = json.load(data)
              table_strings = config
              
              
              class TableView(QtWidgets.QWidget):
              	"""
              	Tableview to test the delegates for multiple rows and columns
              	"""
              
              	def __init__(self):
              		super(TableView, self).__init__()
              		self.ui = Ui_Form()
              		self.ui.setupUi(self)
                              # this is coming from json, you can change to normal list as well
              		self.table_col_names = [col for col in config.keys()]
              		self.table_row_names = [row for row in range(10)]
              		self.empty = [["", ""], ["", ""], ["", ""], ["", ""], ["", ""]]
              		self.model = TableModel(self.table_col_names, self.table_row_names, self.empty)
              		self.ui.tableView.setModel(self.model)
              
              
              app = QtWidgets.QApplication(sys.argv)
              app.setStyle('windows')
              window = TableView()
              window.show()
              sys.exit(app.exec_())
              
              J 1 Reply Last reply 7 Oct 2021, 10:51
              0
              • B blossomsg
                7 Oct 2021, 09:40

                Hey @jeremy_k
                thanks for reply.
                in the ui class i am passing list of empty strings, as i don't want it prefilled
                i assumed model will be enough to explain the situation
                below is the ui code
                i am looking for how to get n-number of rows with insertRows, but no success

                from ui.table_view_delgate import Ui_Form
                from tablemodel import TableModel
                from PyQt5 import QtWidgets
                from PyQt5 import QtGui
                import sys
                import json
                
                with open('configuration_file.json') as data:
                	config = json.load(data)
                table_strings = config
                
                
                class TableView(QtWidgets.QWidget):
                	"""
                	Tableview to test the delegates for multiple rows and columns
                	"""
                
                	def __init__(self):
                		super(TableView, self).__init__()
                		self.ui = Ui_Form()
                		self.ui.setupUi(self)
                                # this is coming from json, you can change to normal list as well
                		self.table_col_names = [col for col in config.keys()]
                		self.table_row_names = [row for row in range(10)]
                		self.empty = [["", ""], ["", ""], ["", ""], ["", ""], ["", ""]]
                		self.model = TableModel(self.table_col_names, self.table_row_names, self.empty)
                		self.ui.tableView.setModel(self.model)
                
                
                app = QtWidgets.QApplication(sys.argv)
                app.setStyle('windows')
                window = TableView()
                window.show()
                sys.exit(app.exec_())
                
                J Offline
                J Offline
                JonB
                wrote on 7 Oct 2021, 10:51 last edited by
                #7

                @blossomsg
                But when you insert a new row you go:

                self.data.insert(position, "")
                

                That is a single string, not an array/list of strings corresponding to each column, which is what you have in your self.empty?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  blossomsg
                  wrote on 7 Oct 2021, 16:32 last edited by
                  #8

                  Hey @JonB ,
                  thanks for replying
                  i guess what you're saying is, this what i am suppose to do?
                  i tried with regular list, nested list, but no success
                  self.data.insert(position, ["", ""])
                  and
                  i even tried vice versa, updated
                  self.empty with ["", ""] and "" -- no success

                  J 1 Reply Last reply 7 Oct 2021, 16:44
                  0
                  • B blossomsg
                    7 Oct 2021, 16:32

                    Hey @JonB ,
                    thanks for replying
                    i guess what you're saying is, this what i am suppose to do?
                    i tried with regular list, nested list, but no success
                    self.data.insert(position, ["", ""])
                    and
                    i even tried vice versa, updated
                    self.empty with ["", ""] and "" -- no success

                    J Offline
                    J Offline
                    JonB
                    wrote on 7 Oct 2021, 16:44 last edited by
                    #9

                    @blossomsg said in insertRows and insertRow in tableview:

                    self.data.insert(position, ["", ""])

                    From what I can in self.empty one row is indeed ["", ""], so that insert() looks better than "".

                    but when i run the above code(model) with ui the 5th row does not appear

                    What is the question/situation/problem? The code you show doesn't actually call any insertRow() or insertRows()? What 5th row?

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      blossomsg
                      wrote on 7 Oct 2021, 17:46 last edited by
                      #10

                      @JonB said in insertRows and insertRow in tableview:

                      one row is indeed

                      What i am looking for,
                      right now there are 0 to 4 rows
                      and 2 columns
                      i want additional rows after 4th row through insertRows() is that possible?and if so how?
                      i don't want to mess with or keep adding again and again empty "" in the self.empty

                      J 1 Reply Last reply 7 Oct 2021, 18:04
                      0
                      • B blossomsg
                        7 Oct 2021, 17:46

                        @JonB said in insertRows and insertRow in tableview:

                        one row is indeed

                        What i am looking for,
                        right now there are 0 to 4 rows
                        and 2 columns
                        i want additional rows after 4th row through insertRows() is that possible?and if so how?
                        i don't want to mess with or keep adding again and again empty "" in the self.empty

                        J Offline
                        J Offline
                        JonB
                        wrote on 7 Oct 2021, 18:04 last edited by JonB 10 Jul 2021, 18:06
                        #11

                        @blossomsg said in insertRows and insertRow in tableview:

                        i want additional rows after 4th row through insertRows() is that possible?and if so how?

                        Of course. Just call self.model.insertRow(5). You seem to have written your method for inserting rows but not called it.

                        i don't want to mess with or keep adding again and again empty "" in the self.empty

                        I don't know what this means. The row you add will be empty because that's how you create it. Whether a row is inserted or not you can update its column values via setData().

                        1 Reply Last reply
                        1
                        • B Offline
                          B Offline
                          blossomsg
                          wrote on 7 Oct 2021, 19:15 last edited by blossomsg 10 Jul 2021, 19:15
                          #12

                          @JonB Thanks mate, you solved the mystery
                          so i am using the below code -- i am creating rows=3 rows of ["", ""] -- as you had corrected me for ""
                          rest everything is same
                          and the biggest part that i missed was self.model.insertRow(5)

                          Of course. Just call self.model.insertRow(5). You seem to have written your method for inserting rows but not called it.
                          

                          I assumed insertRow to work as data and setData , i had no clue we need to call it
                          i had even downloaded the PyQt5 repo -- examples\itemviews\editabletreemodel\editabletreemodel.py
                          it was really confusing

                          	def insertRow(self, position, index=QtCore.QModelIndex()):
                          		self.insertRows(position, 3, index)
                          
                          	def insertRows(self, position, rows, parent=QtCore.QModelIndex()):
                          		print(position, rows, parent)
                          		self.beginInsertRows(parent, position, position + rows - 1)
                          		for rows in range(0, rows):
                          			self.data.insert(position, ["", ""])
                          		self.endInsertRows()
                          		return True
                          
                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            blossomsg
                            wrote on 7 Oct 2021, 19:23 last edited by
                            #13

                            Thank You all
                            But i would like to even add one more thing, as per the docs
                            https://doc.qt.io/qt-5/qabstractitemmodel.html#rowCount
                            When implementing a table based model, rowCount() should return 0 when the parent is valid.

                            and i am doing quite the opposite, i am creating my rows from rowCount()

                            https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRow
                            https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows

                            so just for clarity which is the most preferred method, if there is any example page besides the mvc, please do share.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              ChrisW67
                              wrote on 7 Oct 2021, 22:34 last edited by
                              #14

                              QAbstractItemModel::rowCount() has nothing to do with creating new rows in the model: it returns the current size and, as a const method, cannot modify the model. The documentation note, "When implementing a table based model, rowCount() should return 0 when the parent is valid," means exactly what it says. Top level items in a model have an invalid parent model index. There is no hierarchy in a table model, so there are no children of other items; all items are top level items. If anything calls your rowCount() on a table model and specifies anything other than an invalid index as the parent (it shouldn't, something is buggy) then the function should return 0 as a defensive response.

                              To create a new row or rows in your table model you call insertRow() or insertRows() and specify at which row you wish to insert the new row. The value rowCount() returns changes after this call.

                              1 Reply Last reply
                              1
                              • B Offline
                                B Offline
                                blossomsg
                                wrote on 8 Oct 2021, 12:39 last edited by
                                #15

                                Hey guys,
                                Thanks a bunch, in all i wanted to understand the insertRows and insertRow, plus how to implement it.
                                You guys directed me in right direction
                                Thank You.
                                marking resolved

                                1 Reply Last reply
                                0

                                5/15

                                6 Oct 2021, 20:45

                                topic:navigator.unread, 10
                                • Login

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