updating model columns based on view update
-
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 -
@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?
-
@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?
@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-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.@user4592357 Why is this a problem?
-
@user4592357 Why is this a problem?
@Christian-Ehrlicher
i have one column, which does some heavy calculation for data retrieval, so i wouldn't want extra work be done -
@Christian-Ehrlicher
i have one column, which does some heavy calculation for data retrieval, so i wouldn't want extra work be done@user4592357 You should not do the calculation inside the data() function.
-
@user4592357 You should not do the calculation inside the data() function.
@Christian-Ehrlicher
then where? ifdata()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 -
@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.
-
@Christian-Ehrlicher
then where? ifdata()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@user4592357
In view of @Christian-Ehrlicher's comments, you might consider adding aQProxyModelwhich hides the computed column from the outside world, makes life simpler. -
@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) -
@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)@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.