Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Model within a model?

Tags:

c++

model

qt

qml

I have a Qt model which could very well be a QAbstractListModel. Each "row" represents an object I have stored in a QList. I'm displaying this in QML in a ListView. However, each object has one property that happens to be an array of strings. I would like to display this as a ListView within the delegate that displays that row. But I don't know how to expose that model (for the string array property of the object) to QML. I can't expose it through the data function since Models are QObjects, which cannot be QVariants. I thought of using QAbstractItemModel instead, but I still don't know how to get a model for my ListView. In case it matters, I'm using Qt 5.0.0 release.

like image 301
kanders-fatpot Avatar asked Dec 31 '12 15:12

kanders-fatpot


People also ask

What is a QML delegate?

The delegate provides a template defining each item instantiated by a view. The index is exposed as an accessible index property. Properties of the model are also available depending upon the type of Data Model. filterOnGroup : string. This property holds name of the group that is used to filter the delegate model.

What are Qt models?

Two of the standard models provided by Qt are QStandardItemModel and QFileSystemModel. QStandardItemModel is a multi-purpose model that can be used to represent various different data structures needed by list, table, and tree views. This model also holds the items of data.

What is delegate in Qt?

Detailed Description. QItemDelegate can be used to provide custom display features and editor widgets for item views based on QAbstractItemView subclasses. Using a delegate for this purpose allows the display and editing mechanisms to be customized and developed independently from the model and view.

Is QT a MVC?

Short answer. Qt's MVC only applies to one data structure. When talking about an MVC application you should not think about QAbstractItemModel or QListView . If you want an MVC architecture for your whole program, Qt hasn't such a "huge" model/view framework.


1 Answers

You can return QVariantList from your main QAbstractListModel and this can then be assigned as a model to your internal ListView that you have in the delegate. I have added a small example that has a very simple one row model with internal model as an example.

The c++ model class:

class TestModel : public QAbstractListModel
{
  public:

  enum EventRoles {
    StringRole = Qt::UserRole + 1
  };

  TestModel()
  {
    m_roles[ StringRole] = "stringList";
    setRoleNames(m_roles);
  }

  int rowCount(const QModelIndex & = QModelIndex()) const
  {
    return 1;
  }

  QVariant data(const QModelIndex &index, int role) const
  {
    if(role == StringRole)
    {
      QVariantList list;
      list.append("string1");
      list.append("string2");
      return list;
    }
  }

  QHash<int, QByteArray> m_roles;
};

Now you can set this model to QML and use it like this:

ListView {
  anchors.fill: parent
  model: theModel //this is your main model

  delegate:
    Rectangle {
      height: 100
      width: 100
      color: "red"

      ListView {
        anchors.fill: parent
        model: stringList //the internal QVariantList
        delegate: Rectangle {
          width: 50
          height: 50
          color: "green"
          border.color: "black"
          Text {
            text: modelData //role to get data from internal model
          }
        }
      }
    }
}
like image 76
JuliusG Avatar answered Oct 20 '22 00:10

JuliusG