0

I'm currently working on a customization of QAbstractItemModel an I encountered a problem. The model itself works fine so far, but I have problems, if i try to display it with QTreeView.

The Model itself is able to change its column number on its own, depending on it's data. But the view will never update the number of columns shown, only their content. I did overload insertColumns:

bool MyModel::insertColumns(int column, int count, const QModelIndex &parent)
{
    bool success;
    beginInsertColumns(parent, column, column + count - 1);
    success = this->getItem(parent)->insertColumns(column, count);
    endInsertColumns();
    return success;
}

I experimented a little bit and found out, that if I reset and set the View each time, it will display the right number of columns:

connect(this->model, SIGNAL(columnsChanged()), this->ui->treeView, SLOT(reset()));

But there has to be another way to do this. I'm looking for a function, that will just tell the View that the column-count has changed. But the only one i found (QTreeView::columnCountChanged(int oldCount, int newCount)) is protected...

Felix
  • 6,885
  • 1
  • 29
  • 54
  • There may be an easier approach to your model. What result are you looking for in the view? – davepmiller Sep 24 '14 at 00:00
  • Well, all I want now is that after I increased the Number of coumns, it should display this new number, so basically add one to the view. – Felix Sep 24 '14 at 16:12

1 Answers1

1

here are some other signals that treeview's mode can give out these should all be triggered if your inserting a column so just use the appropriate one and connect to update on your table view, although i would have thought that if your changing the underlying model the view should update, and if it doesnt reset the model

ui->treeView->model()->layoutChanged();
ui->treeView->model()->dataChanged();
ui->treeView->model()->columnsInserted();
ui->treeView->model()->columnsMoved();
AngryDuck
  • 4,358
  • 13
  • 57
  • 91
  • Thanks! `ui->treeView->model()->layoutChanged();` worked fine! And without that it does update the content of the already shown columns, but does not add one. – Felix Sep 24 '14 at 16:11
  • if you add the model to the view again after a column is added then it should be fine then – AngryDuck Sep 24 '14 at 16:14