Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Insert/remove/accessing items on QAbstractListModel
QtWS25 Last Chance

Insert/remove/accessing items on QAbstractListModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 250 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.
  • J Offline
    J Offline
    JohnRS
    wrote on last edited by
    #1

    Hi everyone,

    I need some help related to several operations that I want to do on QAbstractListModel.
    Let's suppose that we have a model like this:
    QAbstractListModel* myModel; // myModel is an array of structs that fills a QML ListView

    The questions:

    1. How can I access the structs variables from, let's say, third element of myModel ?
      QVariant qv = myModel->data(myModel->index(3, 0), Qt::DisplayRole); // this is how I access the third element, but how can I read the data from the structure inside "qv" ?

    2. How can I insert/remove elements in myModel ?
      For removing elements I used this method:
      myModel->removeRow(0, myModel->index(0, 0));
      but apparently doesn't have any effect.

    Can someone tell me what I do wrong?

    Thanks a lot.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @JohnRS said in Insert/remove/accessing items on QAbstractListModel:

      How can I access the structs variables from, let's say, third element of myModel ?
      QVariant qv = myModel->data(myModel->index(3, 0), Qt::DisplayRole); // this is how I access the third element, but how can I read the data from the structure inside "qv" ?

      Use custom roles. One for each field you want to expose.

      @JohnRS said in Insert/remove/accessing items on QAbstractListModel:

      How can I insert/remove elements in myModel ?
      For removing elements I used this method:
      myModel->removeRow(0, myModel->index(0, 0));
      but apparently doesn't have any effect.

      You can add a method to your model that accepts an object of the type you store.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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