{
if(index.isValid() && index.row() < m_fields.size()) {
switch(role) {
case Qt::DisplayRole:
return fieldName(m_fields.at(index.row()).first);
case Qt::CheckStateRole:
return m_fields.at(index.row()).second ? Qt::Checked : Qt::Unchecked;
case fieldRole:
return m_fields.at(index.row()).first;
default:
;
}
}
}
{
bool success = false;
if(index.isValid() && index.row() < m_fields.size()) {
switch(role) {
case Qt::CheckStateRole:
m_fields[index.row()].second = value.toInt() == Qt::Checked;
success = true;
}
break;
case fieldRole:
m_fields[index.row()].first = value.toInt();
success = true;
}
break;
default:
;
}
}
if(success) {
dataChanged(index, index, QVector<int>() << role);
}
return success;
}
// I'm using an user role. I reimplemented itemData to return data for that role as well.
QMap<int, QVariant> FieldModel
::itemData(const QModelIndex &index
) const{
QMap<int, QVariant> roles;
for (int i = 0; i <= fieldRole; ++i) {
if (variantData.isValid())
roles.insert(i, variantData);
}
return roles;
}
bool FieldModel
::setItemData(const QModelIndex &index,
const QMap<int, QVariant>
&roles
) {
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
setData(index, it.value(), it.key());
return true;
// the default implementation (from qabstractitemmodel.cpp)
// bool b = true;
// for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
// b = b && setData(index, it.value(), it.key()); // does not call setData() after setData() returned once false -> thats why I reimplemented the method
// return b;
}
Qt::DropActions FieldModel::supportedDropActions() const
{
return Qt::MoveAction;
}
Qt::DropActions FieldModel::supportedDragActions() const
{
return Qt::MoveAction;
}
bool FieldModel
::insertRows(int row,
int count,
const QModelIndex &parent
) {
if(parent.isValid())
return false;
beginInsertRows(parent, row, 0);
for(int index = row, end = row + count; index < end; ++index) {
m_fields.insert(index, p(KnownField::Invalid, false));
}
endInsertRows();
return true;
}
// since the model has only one column is don't think I need to do here more
bool FieldModel
::insertColumns(int ,
int ,
const QModelIndex &) {
return false;
}
bool FieldModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
// row is always 2 when this method is called
if(parent.isValid())
return false;
if(count) {
beginRemoveRows(parent, row, row + count - 1);
for(int index = row, end = row + count; index < end; ++index) {
m_fields.removeAt(index);
}
endRemoveRows();
}
return true;
}
// since the model has only one column is don't think I need to do here more
bool FieldModel
::removeColumns(int ,
int ,
const QModelIndex &) {
return false;
}
QVariant FieldModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && index.row() < m_fields.size()) {
switch(role) {
case Qt::DisplayRole:
return fieldName(m_fields.at(index.row()).first);
case Qt::CheckStateRole:
return m_fields.at(index.row()).second ? Qt::Checked : Qt::Unchecked;
case fieldRole:
return m_fields.at(index.row()).first;
default:
;
}
}
return QVariant();
}
bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool success = false;
if(index.isValid() && index.row() < m_fields.size()) {
switch(role) {
case Qt::CheckStateRole:
if(value.canConvert(QMetaType::Int)) {
m_fields[index.row()].second = value.toInt() == Qt::Checked;
success = true;
}
break;
case fieldRole:
if(value.canConvert(QMetaType::Int)) {
m_fields[index.row()].first = value.toInt();
success = true;
}
break;
default:
;
}
}
if(success) {
dataChanged(index, index, QVector<int>() << role);
}
return success;
}
// I'm using an user role. I reimplemented itemData to return data for that role as well.
QMap<int, QVariant> FieldModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles;
for (int i = 0; i <= fieldRole; ++i) {
QVariant variantData = data(index, i);
if (variantData.isValid())
roles.insert(i, variantData);
}
return roles;
}
bool FieldModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
{
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
setData(index, it.value(), it.key());
return true;
// the default implementation (from qabstractitemmodel.cpp)
// bool b = true;
// for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
// b = b && setData(index, it.value(), it.key()); // does not call setData() after setData() returned once false -> thats why I reimplemented the method
// return b;
}
Qt::DropActions FieldModel::supportedDropActions() const
{
return Qt::MoveAction;
}
Qt::DropActions FieldModel::supportedDragActions() const
{
return Qt::MoveAction;
}
bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
{
if(parent.isValid())
return false;
beginInsertRows(parent, row, 0);
for(int index = row, end = row + count; index < end; ++index) {
m_fields.insert(index, p(KnownField::Invalid, false));
}
endInsertRows();
return true;
}
// since the model has only one column is don't think I need to do here more
bool FieldModel::insertColumns(int , int , const QModelIndex &)
{
return false;
}
bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
{
// row is always 2 when this method is called
if(parent.isValid())
return false;
if(count) {
beginRemoveRows(parent, row, row + count - 1);
for(int index = row, end = row + count; index < end; ++index) {
m_fields.removeAt(index);
}
endRemoveRows();
}
return true;
}
// since the model has only one column is don't think I need to do here more
bool FieldModel::removeColumns(int , int , const QModelIndex &)
{
return false;
}
To copy to clipboard, switch view to plain text mode
Bookmarks