Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion of column count for tree views

Tags:

c++

qt

In the following example, every child item has only 1 column although it is supposed to have 2 columns.

( MyTreeModel is a subclass of QAbstractItemModel. )

int MyTreeModel::columnCount( const QModelIndex &rParent /*= QModelIndex()*/ ) const
{
    if (rParent.isValid())
    {
           return 2;
    }
    else
    {
        return 1;
    }
}

In the following example, QTreeView show 2 columns for parent items and 1 column for child items as expected.

int MyTreeModel::columnCount( const QModelIndex &rParent /*= QModelIndex()*/ ) const
{
    if (rParent.isValid())
    {
           return 1;
    }
    else
    {
        return 2;
    }
}

So, it seems that the column number of child items is limited by the column number of its parent item. Is it the standard behavior ? Am I doing something wrong ?

like image 257
SRF Avatar asked Feb 19 '14 12:02

SRF


1 Answers

I checked the source code at https://qt.gitorious.org/ (currently not working alternative https://github.com/qtproject/qtbase/blob/dev/src/widgets/) and found the answer as follows:

  1. I checked the method void QTreeView::setModel(QAbstractItemModel *model). There I noticed line d->header->setModel(model);. Header is what you need.
  2. Type of header, it is QHeaderView.
  3. Then I checked the method void QHeaderView::setModel(QAbstractItemModel *model)
  4. There connection is made: QObject::disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int)));
  5. The last thing I did was read the slot method void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)

Guess what I've found there:

void QHeaderView::sectionsInserted(const QModelIndex &parent,
int logicalFirst, int logicalLast)
{
    Q_D(QHeaderView);
    if (parent != d->root)
        return; // we only handle changes in the top level

So only top level items have impact on number of columns.

like image 116
Marek R Avatar answered Oct 11 '22 14:10

Marek R