예제 #1
0
QVariant MetaPropertyModel::data(const QModelIndex &index, int role) const
{
  if (!m_metaObject || !index.isValid()) {
    return QVariant();
  }

  MetaProperty *property = m_metaObject->propertyAt(index.row());
  if (role == Qt::DisplayRole) {
    switch (index.column()) {
    case 0:
      return property->name();
    case 2:
      return property->typeName();
    case 3:
      return property->metaObject()->className();
    }
  }

  if (index.column() == 1) {
    if (!m_object) {
      return QVariant();
    }

    // TODO: cache this, to make this more robust against m_object becoming invalid
    const QVariant value = property->value(m_metaObject->castForPropertyAt(m_object, index.row()));
    switch (role) {
    case Qt::DisplayRole:
      return Util::variantToString(value);
    case Qt::DecorationRole:
      return Util::decorationForVariant(value);
    case Qt::EditRole:
      return value;
    }
  }
  return QVariant();
}