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 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 Online
      J Online
      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 Online
          J Online
          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 Online
              J Online
              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

                      15/15

                      8 Oct 2021, 12:39

                      • Login

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