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 ?
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:
void QTreeView::setModel(QAbstractItemModel *model)
. There I noticed line d->header->setModel(model);
. Header is what you need. QHeaderView
. void QHeaderView::setModel(QAbstractItemModel *model)
QObject::disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int)));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With