Search code examples
c++qtqtreeviewqt5.3

QTreeView update column number


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...


Solution

  • 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();