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. [SOLVED] Some advice on how to pass a modified role value in the ListView to its listmodel?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Some advice on how to pass a modified role value in the ListView to its listmodel?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    open-src
    wrote on last edited by
    #1

    In my qml project, I have created a custom listmodel by subclassing QAbstractListModel, and I use the PictureModel in a ListView. The ListView contains a Textedit element to display the 'comments' role. I am wondering about that when modifying the comments role value, how to pass the value to its model item.
    Any advice will be appreciated!

    here is the header file:

    @class PictureModel : public QAbstractListModel
    {
    Q_OBJECT

    public:
    enum Roles{
    nameRole = Qt::UserRole + 1,
    CommentsRole
    };

    PictureModel(QObject *parent = 0);
    Qt::ItemFlags flags(const QModelIndex &index) const;
    
    void addPicture(const Picture &picture);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
    
    bool setData(const QModelIndex &index, const QVariant &value,
                 int role = Qt::EditRole);
    bool insertRows(int row, int count, const QModelIndex &parent);
    bool removeRows(int row, int count, const QModelIndex &parent);
    

    private:
    QList<Picture> m_pictures;
    };

    @
    here is the part of cpp file:

    @bool PictureModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    if(index.isValid()&&role == Qt::EditRole){
    m_pictures.replace(index.row(), value.value<Picture>());
    emit dataChanged(index,index);
    return true;
    }

    return false;
    

    }@

    here is the delegate of the ListView object.
    @
    Component{
    id:listdelegate
    Rectangle{
    id:single
    .............................
    TextEdit {

            id:edit
            width: parent.width
            text: comments
            font.pointSize: 18
            color: "black"
            opacity: 0
            focus: true
            wrapMode :TextEdit.Wrap
        }
    

    .............................
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      apap_
      wrote on last edited by
      #2

      add a Q_INVOKABLE function in your model so that you can call it from qml. I think about something like this :
      @
      Q_INVOKABLE void editComments(int listIndex, const QVariant &comments)
      {
      QModelIndex qModelIndex = index(listIndex, 0);
      bool result = setData(qModelIndex, comments, CommentsRole);
      ...
      }
      @

      1 Reply Last reply
      0
      • O Offline
        O Offline
        open-src
        wrote on last edited by
        #3

        I got it, thanks a lot!

        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