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. updating model columns based on view update
Qt 6.11 is out! See what's new in the release blog

updating model columns based on view update

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.9k 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have a tree view with 5 column, i created a context menu with checkable items to show/hide columns.

    i create the model with column names:

    class TreeModel : public QAbstractItemModel
    {
    public:
         explicit TreeModel(QList<QVariant> lstColumnNames, QObject *parent = nullptr) noexcept
                : QAbstractItemModel(parent)
                , m_lstColumnNames(std::move(lstColumnNames))
    
    private:
         	QList<QVariant> m_lstColumnNames;
    

    the reason i do this is to get tree header names and model column count:

    QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole */) const
    {
    	return orientation == Qt::Horizontal && role == Qt::DisplayRole
    		? m_lstColumnNames[section]
    		: QAbstractItemModel::headerData(section, orientation, role);
    }
    
    int TreeModel::columnCount(const QModelIndex &parent /* = QModelIndex() */) const
    {
    	return parent.isValid()
    		? static_cast<TreeNode *>(parent.internalPointer())->columnCount()
    		: m_lstColumnNames.size();
    }
    

    now, the problem is that, when i hide a column from context menu (and have 4 columns instead of 5), this list in model is not changed, i.e. columnCount() still returns the old 5.

    one option i think of is to have list of pairs, QList<QPair<QVariant, bool>> m_lstColumnNames;, where the bool indicates whether the column is hidden or shown. that means i will need to update the column states every time i hide/show column from gui. also, to return column count i'd need to run over all columns and collect only visible ones.

    i'd like to know what else can i do? probably something better.
    thanks

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @user4592357 said in updating model columns based on view update:

      this list in model is not changed, i.e. columnCount() still returns the old 5.

      That's absolutely correct - you only hide a column in the view, you don't change the model. Why should the model change it's columnCount at all?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      U 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @user4592357 said in updating model columns based on view update:

        this list in model is not changed, i.e. columnCount() still returns the old 5.

        That's absolutely correct - you only hide a column in the view, you don't change the model. Why should the model change it's columnCount at all?

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in updating model columns based on view update:

        Why should the model change it's columnCount at all?

        the problem is that, since model column count is the same, data() is called for those hidden columns.

        Christian EhrlicherC 1 Reply Last reply
        0
        • U user4592357

          @Christian-Ehrlicher said in updating model columns based on view update:

          Why should the model change it's columnCount at all?

          the problem is that, since model column count is the same, data() is called for those hidden columns.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @user4592357 Why is this a problem?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          U 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @user4592357 Why is this a problem?

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            i have one column, which does some heavy calculation for data retrieval, so i wouldn't want extra work be done

            Christian EhrlicherC 1 Reply Last reply
            0
            • U user4592357

              @Christian-Ehrlicher
              i have one column, which does some heavy calculation for data retrieval, so i wouldn't want extra work be done

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @user4592357 You should not do the calculation inside the data() function.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              U 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @user4592357 You should not do the calculation inside the data() function.

                U Offline
                U Offline
                user4592357
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                then where? if data() wants to retrieve that model index info, i need to provide the correct data, since i don't know whether column is hidden in gui or not

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @user4592357 said in updating model columns based on view update:

                  then where?

                  When you fill the data into the model for example. data() is called very often and should do as less work as possible.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • U user4592357

                    @Christian-Ehrlicher
                    then where? if data() wants to retrieve that model index info, i need to provide the correct data, since i don't know whether column is hidden in gui or not

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @user4592357
                    In view of @Christian-Ehrlicher's comments, you might consider adding a QProxyModel which hides the computed column from the outside world, makes life simpler.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      user4592357
                      wrote on last edited by user4592357
                      #10

                      @Christian-Ehrlicher
                      okay, now i add the value of that column in a method filling tree node, only if the column is to be shown, because the computation of that data is slow.
                      now, if the column is hidden, and i show it, it doesn't display the data (because the logic of filling the node was done somewhere else)

                      JonBJ 1 Reply Last reply
                      0
                      • U user4592357

                        @Christian-Ehrlicher
                        okay, now i add the value of that column in a method filling tree node, only if the column is to be shown, because the computation of that data is slow.
                        now, if the column is hidden, and i show it, it doesn't display the data (because the logic of filling the node was done somewhere else)

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @user4592357
                        I don't understand. If you allow for allow for hidden columns becoming shown, you have to write the logic to ensure the data is available. If necessary, react to the column being hidden/shown by refreshing the model or changing it or whatever is required to fetch the necessary data.

                        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