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. How do deal with external changes to a model
QtWS25 Last Chance

How do deal with external changes to a model

Scheduled Pinned Locked Moved Unsolved General and Desktop
viewsmodelscolumnsqt 5qt widget
4 Posts 2 Posters 615 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.
  • F Offline
    F Offline
    Folling
    wrote on 18 Nov 2019, 00:54 last edited by Folling
    #1

    I have a QAbstractItemModel that represents a list of items from a class looking like this:

    class item {
    public:
        QHash<category, QVariant> values;
    };
    

    These items belong to a a parent class called register, which simultaneously has a list of category objects.
    Each item has a value for each category - categories can be added and removed using a separate QAbstractItemModel subclass.

    Now in a table, all items of a register should be layed out by having one column for each category, where a row corresponds with one item.

    My issue now is - adding a new category in register doesn't go through the item_model, but instead through the category_model, so I'm unsure how to add a column to the item_model.

    I can of course just override insertColumns like this:

    bool item_model::insertColumns(int column, int count, QModelIndex const& parent) {
        beginInsertColumns(parent, column, count);
        endInsertColumns();
    
        return true;
    }
    

    and then execute it like this:

    category_model->insertRows(args);
    item_model->insertColumns(args);
    

    But that seems completely nonsensical. My question is thus - how should I go about adding the columns to the item model?

    V 1 Reply Last reply 18 Nov 2019, 09:04
    0
    • F Folling
      18 Nov 2019, 00:54

      I have a QAbstractItemModel that represents a list of items from a class looking like this:

      class item {
      public:
          QHash<category, QVariant> values;
      };
      

      These items belong to a a parent class called register, which simultaneously has a list of category objects.
      Each item has a value for each category - categories can be added and removed using a separate QAbstractItemModel subclass.

      Now in a table, all items of a register should be layed out by having one column for each category, where a row corresponds with one item.

      My issue now is - adding a new category in register doesn't go through the item_model, but instead through the category_model, so I'm unsure how to add a column to the item_model.

      I can of course just override insertColumns like this:

      bool item_model::insertColumns(int column, int count, QModelIndex const& parent) {
          beginInsertColumns(parent, column, count);
          endInsertColumns();
      
          return true;
      }
      

      and then execute it like this:

      category_model->insertRows(args);
      item_model->insertColumns(args);
      

      But that seems completely nonsensical. My question is thus - how should I go about adding the columns to the item model?

      V Offline
      V Offline
      VRonin
      wrote on 18 Nov 2019, 09:04 last edited by VRonin
      #2

      @Folling said in How do deal with external changes to a model:

      doesn't go through the item_model, but instead through the category_model

      What are these models?

      I can of course just override insertColumns like this:

      No you can't. At the time beginInsertColumns is called, columnCount() must return a number of columns inferior by exactly count to when columnCount() is called after endInsertColumns() is executed. Also, the 3rd argument you are passing to beginInsertColumns() is just wrong, it should be column+count-1

      "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
      0
      • F Offline
        F Offline
        Folling
        wrote on 18 Nov 2019, 19:33 last edited by
        #3

        What are these models?

        They are subclasses of QAbstractItemModel that wrap around the specific item and category objects to provide access to them in list and tableviews.

        No you can't. At the time beginInsertColumns is called, columnCount() must return a number of columns inferior by exactly count to when columnCount() is called after endInsertColumns() is executed.

        Interesting - I didn't know that, however, in that case I could theoretically expose the two functions.

        it should be column+count-1

        Pardon me on that - I wrote the parameters out from my head, since I don't use that approach.

        My current approach is to emit signals from register whenever a category is added, and connect that signal in my item_model. Speaking of that however - that would face the same issue as you mentioned before, given that columnCount() would return the same result prior and after - however it does work as of right now, so perhaps you were mistaken or it's working by accident.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Folling
          wrote on 27 Nov 2019, 06:44 last edited by Folling
          #4

          An update on this from my side:

          My initial approach wasn't flawless, as @VRonin pointed out, because QAbstractItemModel::columnCount didn't return the appropriate values. I've fixed this, by not immediately passing the new count after inserting the elements to a list, of which the size was used, but instead have the model save exactly how many categories it currently accounts for, so the connection looks like this:

          connect(item_registry.get(), &item_registry::category_added, [this](int position, QSharedPointer<category>) {
              beginInsertColumns(QModelIndex{}, position, position);
              _current_category_count++;
              endInsertColumns();
          });
          

          and likewise columnCount:

          int item_model::columnCount(QModelIndex const&) const {
              if(!_register) {
                  qCritical() << "Item-model didn't have a register";
                  return 0;
              }
          
              return 1 /* name */ + _current_category_count;
          }
          
          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