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. Model for custom object
Forum Updated to NodeBB v4.3 + New Features

Model for custom object

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.1k Views 3 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.
  • P Offline
    P Offline
    Patrick Wright
    wrote on last edited by Patrick Wright
    #1

    Hello,

    I have been searching and reading documentation in earnest to figure out how to create a model (to be used in a table view) for a list of custom objects. My general scenario is this: assuming I have an QList of objects of a custom class, I would like to create a model which will allow these objects to be displayed in a table where each row is an object and each column is a member variable.

    I have already implemented rowCount(), columnCount(), data(), and headerData() and am able to get data to be displayed in the table when I initially perform setModel() on the QTableView. My model doesn't hold the actual data but only a pointer to a QList. For example, my constructor is

    PortModel(QList<AbstractPortInfo *> *infos);
    

    The problem I am having is figuring out how to update the view when I append/remove/change objects in the List. I have read that you should emit dataChanged() which I am doing as follows

    emit dataChanged(index(infos->length() - 1, 0), index(infos->length() - 1, 1));
    //Assuming the AbstractPortInfo object has 2 members wish to return
    

    when I append an item, however this does nothing.

    I don't want to hold the data in the model itself because I am using the array of objects elsewhere in scenarios where I don't need a whole model.

    Any ideas as to how this can be accmplished?
    Thanks in advance!

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

      Hi,

      How are you adding data to your list ?
      How do you propagate that information to the upper layers?

      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
      • P Offline
        P Offline
        Patrick Wright
        wrote on last edited by
        #3

        When I use the QList outside of the widget which has the QTableView (i.e. when I don't care about displaying the data), I simply use the append() method of the list. From within the widget with the view, I added an append() method as such

        void append(AbstractPortInfo *info);
        

        to the model so that I know when an update occurs to potentially tell the view to update.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          You must inform the item model of any changes you make to your own model.
          It should look something like this:

          void append(AbstractPortInfo *info)
          {
          beginInsertRows(QModelIndex(),infos->count(),infos->count());
          infos->append(info);
          endInsertRows();
          }
          

          Look at the protected methods of QAbstractItemModel Class for more.

          P 1 Reply Last reply
          3
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            dataChanged only notifies that the data contained in an existing item changed.
            You need to use the begin*()/end*() methods. unfortunately this makes:

            don't want to hold the data in the model itself.

            difficult because those methods are protected and you need to add public methods (even slots) that call those private methods

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • M mpergand

              You must inform the item model of any changes you make to your own model.
              It should look something like this:

              void append(AbstractPortInfo *info)
              {
              beginInsertRows(QModelIndex(),infos->count(),infos->count());
              infos->append(info);
              endInsertRows();
              }
              

              Look at the protected methods of QAbstractItemModel Class for more.

              P Offline
              P Offline
              Patrick Wright
              wrote on last edited by
              #6

              @mpergand

              This works like a charm! I knew it couldn't be too difficult to achieve, I was just a bit confused.

              Thank You!

              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