Notify QTableView to update data
-
Hello.
I have data source with its own interface. It hase methods like add(QString &name), remove(QString &name) and others.
Also I have model which is inherited from QAbstractTableModel, it implements all required methods. Model connects to data source and receive data via its interface.
And I have QTableView which is connected to my model.
After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView? -
Hello.
I have data source with its own interface. It hase methods like add(QString &name), remove(QString &name) and others.
Also I have model which is inherited from QAbstractTableModel, it implements all required methods. Model connects to data source and receive data via its interface.
And I have QTableView which is connected to my model.
After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?@Pavel-Romankov said in Notify QTableView to update data:
Also I have model which is inherited from QAbstractTableModel, it implements all required methods.
After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?Have you read https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing, which tells you what you need to implement in your subclass? For example,
add()
ing will invokeinsertRows()
. When your implementation correctly callsbegin
/endInsertRows()
any attached views will update correctly; if you fail to implement this correctly they will not. -
@Pavel-Romankov said in Notify QTableView to update data:
Also I have model which is inherited from QAbstractTableModel, it implements all required methods.
After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?Have you read https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing, which tells you what you need to implement in your subclass? For example,
add()
ing will invokeinsertRows()
. When your implementation correctly callsbegin
/endInsertRows()
any attached views will update correctly; if you fail to implement this correctly they will not.void beginInsertRows(const QModelIndex &parent, int first, int last);
requires first and last parameters. But in my case I dont know them. When I add item to data source by
dataSource.add("new item")
it doesnt return number of row.
-
void beginInsertRows(const QModelIndex &parent, int first, int last);
requires first and last parameters. But in my case I dont know them. When I add item to data source by
dataSource.add("new item")
it doesnt return number of row.
@Pavel-Romankov said in Notify QTableView to update data:
But in my case I dont know them
You must know them, since it is your own model! You know where you add the row(s), you need to issue the correct corresponding call.
-
@Pavel-Romankov said in Notify QTableView to update data:
But in my case I dont know them
You must know them, since it is your own model! You know where you add the row(s), you need to issue the correct corresponding call.
@JonB
So i need:- add new item with
dataSorce.add("item");
- get row number of new item.
- notify model about this. In dataSource
emit rowInserted(int rowNumber);
- in model call
beginInsertRows(QModelIndex(), rowNumber, rowNumber + 1); endInsertRows();
right?
-
@JonB
So i need:- add new item with
dataSorce.add("item");
- get row number of new item.
- notify model about this. In dataSource
emit rowInserted(int rowNumber);
- in model call
beginInsertRows(QModelIndex(), rowNumber, rowNumber + 1); endInsertRows();
right?
@Pavel-Romankov
You should put your adding into your model call (dataSorce.add("item")
?) between thebegin
/endInsertRows()
.I believe for inserting one row your call should be
beginInsertRows(QModelIndex(), rowNumber, rowNumber);
not
rowNumber + 1
forlast
, that should be the row number it will end up as:first and last are the row numbers that the new rows will have after they have been inserted.
I am drawing a mental blank on
emit rowInserted(int rowNumber);
, I don't think you do anything for that, doesn'tQAbstractTableModel
do that for you fromendInsertRows()
? TBH I'm not sure, see https://forum.qt.io/topic/66484/how-to-update-qtableview-when-rows-are-inserted-to-it. Maybe see whether that signal gets emitted for you or whether you have to do it....