Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically adding a new row to a QAbstractListModel subclass

Tags:

qt

qlistview

Within an already-instantiated QAbstractListModel subclass, how do I add a row with data in each column, and have the associated QListView display the new row?

It seems that the only way to do it is to reimplement insertRow and setData within my model, and then hack them together in some sort of sequence within another function to add a row. Must I do this? Surely Qt must make it easier to add a new row.

Thanks much! --Dany.

like image 337
Dany Joumaa Avatar asked Jan 16 '11 00:01

Dany Joumaa


1 Answers

Just change your model's data storage, in between beginInsertRows() and endInsertRows().

For instance, let's say you have a flat list model and your model stores the data internally in a QVector m_data. You want to prepend the list, i.e. insert a row at position 0:

beginInsertRows( QModelIndex(), 0, 0 ); //notify views and proxy models that a line will be inserted
m_data.prepend( somedata ); // do the modification to the model data
endInsertRows(); //finish insertion, notify views/models
like image 62
Frank Osterfeld Avatar answered Oct 11 '22 01:10

Frank Osterfeld