PDA

View Full Version : Update QAbstractTableModel?



adutzu89
3rd June 2014, 16:20
I made use of QAbstractTableModel to show data retrieved through QNetworkAccessManager in a QTableView, it works well, the only issue is that if the data is changed the model doesn't "refresh" until I click on the tableview or scroll through the table.

header:

class ModelLClienti:public QAbstractTableModel{
Q_OBJECT
public:
ModelLClienti(QObject *parent=0);

int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
void setLista(QList<QString> lista);
private:
QList<QString> listaCli;
};

source:

ModelLClienti::ModelLClienti(QObject *parent):QAbstractTableModel(parent){
}

int ModelLClienti::rowCount(const QModelIndex &parent) const{
Q_UNUSED(parent);
return listaCli.size()-1;
}
int ModelLClienti::columnCount(const QModelIndex &parent) const{
Q_UNUSED(parent);
return 2;
}
QVariant ModelLClienti::data(const QModelIndex &index, int role) const{
QStringList l;

if (!index.isValid())
return QVariant();

if (index.row() >= listaCli.size() || index.row() < 0)
return QVariant();

if (role == Qt::DisplayRole) {
l=listaCli.at(index.row()).split(',');
if(index.column()==0)
return l[0];
if (index.column() == 1)
return l[1].toUpper();
}
return QVariant();
}
QVariant ModelLClienti::headerData(int section, Qt::Orientation orientation, int role) const{
if (role != Qt::DisplayRole)
return QVariant();

if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("clientId");
case 1:
return tr("Client");
default:
return QVariant();
}
}
return QVariant();
}
void ModelLClienti::setLista(QList<QString> lista){
listaCli=lista;
}

I need to change the data inside the model and afterwards, show in the tableview.

wysota
4th June 2014, 08:37
When you change data in the model, you have to emit dataChanged() signal for the index range that gets changed.

adutzu89
4th June 2014, 21:17
Thank you, I managed it.