insertRows and insertRow in tableview
-
wrote on 7 Oct 2021, 09:40 last edited by
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 successfrom 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_())
-
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 successfrom 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_())
wrote on 7 Oct 2021, 10:51 last edited by@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
? -
wrote on 7 Oct 2021, 16:32 last edited by
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 -
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 successwrote on 7 Oct 2021, 16:44 last edited by@blossomsg said in insertRows and insertRow in tableview:
self.data.insert(position, ["", ""])
From what I can in
self.empty
one row is indeed["", ""]
, so thatinsert()
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()
orinsertRows()
? What 5th row? -
wrote on 7 Oct 2021, 17:46 last edited by
@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 theself.empty
-
@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 theself.empty
wrote on 7 Oct 2021, 18:04 last edited by JonB 10 Jul 2021, 18:06@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()
. -
wrote on 7 Oct 2021, 19:15 last edited by blossomsg 10 Jul 2021, 19:15
@JonB Thanks mate, you solved the mystery
so i am using the below code -- i am creatingrows=3
rows of["", ""]
-- as you had corrected me for""
rest everything is same
and the biggest part that i missed wasself.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 asdata
andsetData
, i had no clue we need to call it
i had even downloaded the PyQt5 repo -- examples\itemviews\editabletreemodel\editabletreemodel.py
it was really confusingdef 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
-
wrote on 7 Oct 2021, 19:23 last edited by
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#insertRowsso just for clarity which is the most preferred method, if there is any example page besides the mvc, please do share.
-
wrote on 7 Oct 2021, 22:34 last edited by
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.
-
wrote on 8 Oct 2021, 12:39 last edited by
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
15/15