Livello di programmazione aggiungendo una nuova riga a una sottoclasse QAbstractListModel

StackOverflow https://stackoverflow.com/questions/4702972

  •  11-10-2019
  •  | 
  •  

Domanda

In una sottoclasse QAbstractListModel già istanziato, come faccio a aggiungere una riga con i dati in ogni colonna, e hanno il QListView associato visualizzare la nuova riga?

Sembra che l'unico modo per farlo è quello di reimplementare insertRow e setData nel mio modello, e poi incidere insieme in una sorta di sequenza all'interno di un'altra funzione per aggiungere una riga. Devo fare questo? Sicuramente Qt deve rendere più facile per aggiungere una nuova riga.

Grazie mille! --Dany.

È stato utile?

Soluzione

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

Altri suggerimenti

I'm afraid you have to do it that way. From the docs:

Models that provide interfaces to resizable list-like data structures can provide implementations of insertRows() and removeRows().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top