Solved Update QHeaderView section count to reflect changes in the model
-
Thanks for the suggestion, but it doesn't look to have an effect
I did the following:
std::cout << "model_columns=" << _model.columnCount() << " tree_columns=" << _tree.header()->count() << "\n"; if (_model.columnCount() != _tree.header()->count()) { for (int i = 0; i < _model.columnCount(); ++i) { if (_tree.columnWidth(i) == 0) _tree.setColumnWidth(i, 10); } _tree.header()->headerDataChanged(Qt::Horizontal, 0, _model.columnCount()); // <-- your suggestion } std::cout << "model_columns=" << _model.columnCount() << " tree_columns=" << _tree.header()->count() << "\n";
The columns remain hidden, and the output is as follows:
model_columns=31 tree_columns=28 model_columns=31 tree_columns=28
-
@skebanga
Ok, I admit it was not a very clever suggestion. Maybe better is to try to signal the view (from the model) that header data should be updated, not emitting the view's signals directly. Have you tried the model's QAbstractItemModel::headerDataChanged signal? For example:void Model::signalAddedRows(Qt::Orientation orientation) { emit headerDataChanged(orientation, 0, columnCount()); }
-
That's it! Thank you!
All that is required is to emit the
headerDataChanged
signal from the modelif (_model.columnCount() != _tree.header()->count()) emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
The newly added columns appear now.
Additional feedback of the change propagating is evidenced by connecting to the
QHeaderView::sectionCountChanged
slot:if (_model.columnCount() != _tree.header()->count()) { // just to prove this is working connect(_tree.header(), &QHeaderView::sectionCountChanged, [](int old_count, int new_count) { std::cout << "sectionCountChanged: " << old_count << " -> " << new_count << "\n"; }); emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount()); }
Results in the following on
stdout
:sectionCountChanged: 28 -> 31
-
@skebanga
You're welcome, I'm glad it worked. -
Hi,
Out of curiosity, are you emitting the signal from outside of the model ?
-
Out of curiosity, are you emitting the signal from outside of the model ?
I am, as detailed in my previous reply:
emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
AFAIK
emit
is just syntactic sugar, and doesn't actually do anything.So the same effect could be had by just doing:
_model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
I leave
emit
in just to show that it's asignal
I'm calling rather than a vanilla member functionPS: Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds
-
@skebanga said:
Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds
Now you do. Cheers.
-
:) thanks!
-
Indeed emit is a macro that turns empty. The point is that from a coding point of view, you don't "emit from another class". Signals are emitted from within the class or the sub-class you just wrote.
Doing like you do right now makes the code confusing and also breaks the single responsibility principle i.e. why would your containing widget play with "internals" of your model ?
-
@SGaist Fair enough - point noted, thanks for the suggestion
-
one thing to note though, re emitting a signal from externally to the model, is that this is an interplay between the view and the model.
The model shouldn't need to know anything about the view, but in this case, it's the fact that the model's
columnCount()
has increased, whilst the view's hasn't.In terms of best practice, would you suggest anything in this case?
-
Technically the "last number of columns" is rather a model specific data. Since that part can change, the model should check the last value and then emit the signal accordingly.
The section ordering part, on the other hand, is indeed view specific data and the model shouldn't do anything with them.