Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. A good way to allow editing of Model items?
QtWS25 Last Chance

A good way to allow editing of Model items?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dynamite101
    wrote on last edited by
    #1

    hey i've been learning normal Qt for over 7 months and i only just started learning some Qml after hearing so much about it. I've been trying to get a better understanding of data models and views in both normal Qt and Qml. Following the standard Qml approach of using a Rectangle and Text element in the delegate, what is a good way to let a user edit the value of text in the delegate's Text element?

    I was wondering is this where the normal Qt will come in? as in don't use qml's ListModel, but instead a model from Qt ? but then i realized the editing still needs to take place on the qml screen, so im still wondering how i can let a user edit the model's data item/role's value?

    Also ive heard its better to subclass a Qt model if the application is too complex for Qml. Could anyone please give me an idea of such a situation?

    1 Reply Last reply
    0
    • W Offline
      W Offline
      weiyuemin
      wrote on last edited by
      #2

      I think it's convenient to operate ListModel (modify and so on) using Qml:
      http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-listmodel.html

      1 Reply Last reply
      0
      • N Offline
        N Offline
        njeisecke
        wrote on last edited by
        #3

        Models should be part of the business logic and belong to C++.

        Accessing model data is currently readonly. You must provide a Q_INVOKABLE setData method in your model if you want to modify data in a C++ model.

        @
        Q_INVOKABLE void setDataForRow(int row, const QVariant &data, const QString &role)
        {
        int role = roleNames().key(role); // this is slow ;-(
        switch (role) {
        // put data into model here, don't forget to emit dataChanged if data has actually changed
        }
        }
        @

        Instead of providing the role as a string you can also use Q_ENUMS to make your Roles visible to QML:

        @
        class MyModel : public QAbstractListModel {
        // ...
        Q_ENUMS(MyRole)
        // ...
        public:
        enum MyRole {
        FirstRole = Qt::UserRole + 1000,
        SecondRole
        }
        }

        Q_INVOKABLE void setDataForRow(int row, const QVariant &data, int role)
        {
        switch (role) {
        // put data into model here
        }
        }
        @

        Delegate Example

        @
        ListView {
        anchors.fill: parent
        model: MyModel {}
        delegate: Row {
        TextInput {
        text: model.firstRole
        onTextChanged: model.setDataForRow(model.index, text, "firstRole" /* or MyModel.FirstRole */)
        }
        }
        }
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dynamite101
          wrote on last edited by
          #4

          [quote author="njeisecke" date="1368618421"]Models should be part of the business logic and belong to C++.

          Accessing model data is currently readonly. You must provide a Q_INVOKABLE setData method in your model if you want to modify data in a C++ model.

          [/quote]

          Thank's so much for your response and detailed reply neisecke.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved