lvdong
23rd November 2010, 08:05
I used a QTableView and an editable QAbstractTableModel subclass to display a large dataset.
But when I double click an item, the original data of the item is gone,
how can I edit the orginal data.
thanks.
model.h
class Model : public QAbstractTableModel
{
Q_OBJECT
public:
Model(QObject *parent = 0);
void setSourceData(const QString &fileName); // to get data
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role);
QVariant headerData(int section, Qt::Orientation orientation,
int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
private:
QStringList rowNames;
QStringList columnNames;
QVector<QVector<int> > dataset;
};
model.cpp(fragment)
void Model::setSourceData(const QString &fileName)(const QString &fileName)
{
// read in data using QTextStream
}
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignRight | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole)
{
return dataset[index.row()][index.column()];
}
// problem solved, I forgot the Qt::EditRole
return QVariant();
}
bool Model::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
if (value.toString().isEmpty()) return false;
dataset[index.row()][index.column()] = value.toInt();
QModelIndex transposedIndex = createIndex(index.column(),
index.row());
emit dataChanged(index, index);
emit dataChanged(transposedIndex, transposedIndex);
return true;
}
return false;
}
Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags |= (Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled);
return flags;
}
main.cpp(fragment)
Model model;
model.setSourceData("data.txt");
QTableView tableView;
tableView.setModel(&model);
But when I double click an item, the original data of the item is gone,
how can I edit the orginal data.
thanks.
model.h
class Model : public QAbstractTableModel
{
Q_OBJECT
public:
Model(QObject *parent = 0);
void setSourceData(const QString &fileName); // to get data
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role);
QVariant headerData(int section, Qt::Orientation orientation,
int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
private:
QStringList rowNames;
QStringList columnNames;
QVector<QVector<int> > dataset;
};
model.cpp(fragment)
void Model::setSourceData(const QString &fileName)(const QString &fileName)
{
// read in data using QTextStream
}
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignRight | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole)
{
return dataset[index.row()][index.column()];
}
// problem solved, I forgot the Qt::EditRole
return QVariant();
}
bool Model::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
if (value.toString().isEmpty()) return false;
dataset[index.row()][index.column()] = value.toInt();
QModelIndex transposedIndex = createIndex(index.column(),
index.row());
emit dataChanged(index, index);
emit dataChanged(transposedIndex, transposedIndex);
return true;
}
return false;
}
Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags |= (Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled);
return flags;
}
main.cpp(fragment)
Model model;
model.setSourceData("data.txt");
QTableView tableView;
tableView.setModel(&model);