Simplify the example by removing the optional roles property

master
Jos van den Oever 2018-05-19 16:46:48 +02:00
parent e3bfa85849
commit 9822dcef07
2 changed files with 6 additions and 27 deletions

View File

@ -19,13 +19,11 @@
"itemProperties": { "itemProperties": {
"completed": { "completed": {
"type": "bool", "type": "bool",
"write": true, "write": true
"roles": [ [ "display" ] ]
}, },
"description": { "description": {
"type": "QString", "type": "QString",
"write": true, "write": true
"roles": [ [], [ "display" ] ]
} }
}, },
"functions": { "functions": {

View File

@ -52,7 +52,7 @@ extern "C" {
} }
int Todos::columnCount(const QModelIndex &parent) const int Todos::columnCount(const QModelIndex &parent) const
{ {
return (parent.isValid()) ? 0 : 2; return (parent.isValid()) ? 0 : 1;
} }
bool Todos::hasChildren(const QModelIndex &parent) const bool Todos::hasChildren(const QModelIndex &parent) const
@ -77,7 +77,7 @@ bool Todos::removeRows(int row, int count, const QModelIndex &)
QModelIndex Todos::index(int row, int column, const QModelIndex &parent) const QModelIndex Todos::index(int row, int column, const QModelIndex &parent) const
{ {
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) { if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) {
return createIndex(row, column, (quintptr)row); return createIndex(row, column, (quintptr)row);
} }
return QModelIndex(); return QModelIndex();
@ -110,9 +110,6 @@ Qt::ItemFlags Todos::flags(const QModelIndex &i) const
if (i.column() == 0) { if (i.column() == 0) {
flags |= Qt::ItemIsEditable; flags |= Qt::ItemIsEditable;
} }
if (i.column() == 1) {
flags |= Qt::ItemIsEditable;
}
return flags; return flags;
} }
@ -156,18 +153,11 @@ QVariant Todos::data(const QModelIndex &index, int role) const
switch (index.column()) { switch (index.column()) {
case 0: case 0:
switch (role) { switch (role) {
case Qt::DisplayRole:
case Qt::UserRole + 0: case Qt::UserRole + 0:
return QVariant::fromValue(completed(index.row())); return QVariant::fromValue(completed(index.row()));
case Qt::UserRole + 1: case Qt::UserRole + 1:
return QVariant::fromValue(description(index.row())); return QVariant::fromValue(description(index.row()));
} }
case 1:
switch (role) {
case Qt::DisplayRole:
case Qt::UserRole + 1:
return QVariant::fromValue(description(index.row()));
}
} }
return QVariant(); return QVariant();
} }
@ -198,7 +188,7 @@ bool Todos::setHeaderData(int section, Qt::Orientation orientation, const QVaria
bool Todos::setData(const QModelIndex &index, const QVariant &value, int role) bool Todos::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
if (index.column() == 0) { if (index.column() == 0) {
if (role == Qt::DisplayRole || role == Qt::UserRole + 0) { if (role == Qt::UserRole + 0) {
if (value.canConvert(qMetaTypeId<bool>())) { if (value.canConvert(qMetaTypeId<bool>())) {
return setCompleted(index.row(), value.value<bool>()); return setCompleted(index.row(), value.value<bool>());
} }
@ -209,13 +199,6 @@ bool Todos::setData(const QModelIndex &index, const QVariant &value, int role)
} }
} }
} }
if (index.column() == 1) {
if (role == Qt::DisplayRole || role == Qt::UserRole + 1) {
if (value.canConvert(qMetaTypeId<QString>())) {
return setDescription(index.row(), value.value<QString>());
}
}
}
return false; return false;
} }
@ -256,7 +239,7 @@ Todos::Todos(QObject *parent):
}, },
[](Todos* o, quintptr first, quintptr last) { [](Todos* o, quintptr first, quintptr last) {
o->dataChanged(o->createIndex(first, 0, first), o->dataChanged(o->createIndex(first, 0, first),
o->createIndex(last, 1, last)); o->createIndex(last, 0, last));
}, },
[](Todos* o) { [](Todos* o) {
o->beginResetModel(); o->beginResetModel();
@ -291,8 +274,6 @@ Todos::~Todos() {
} }
} }
void Todos::initHeaderData() { void Todos::initHeaderData() {
m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("completed"));
m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("description"));
} }
quint64 Todos::activeCount() const quint64 Todos::activeCount() const
{ {