Hi all experts,
First apologize for my poor English.
I have searched related threads but still have no solution.
My goal is to display a customed widget in QTreeView's last column.
I have two solultions to achieve that:
1. use QItemDelegate::createEditor, setEditorData, setModelData, updateEditorGeometry...
2. use QAbstractItemView::setIndexWidge
Because the UI spec requires that the customed widget should always get displayed, no matter it is in edit status, I prefer the solution 2 - use QAbstractItemView::setIndexWidge.
Now my question is when and where to invoke QAbstractItemView::setIndexWidge.
My tree is QTreeView+QAbstractItemModel structure. I subclasses QTreeView andQAbstractItemModel, and I initialize my model's data from a ABClist.
code:
const
{
if (!hasIndex(row, column, parent))
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
{
index=createIndex(row, column, childItem);
return index;
}
else
}
{
if (!index.isValid())
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return createIndex(parentItem->row(), 0, parentItem);
}
{
if (!index.isValid())
if (role != Qt::DisplayRole)
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
{
QModelIndex index;
index=createIndex(row, column, childItem);
return index;
}
else
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
To copy to clipboard, switch view to plain text mode
The approach to add data into my model is to add/modify the ABClist and then invoke QAbstractItemModel::reset.
Now it looks perfect to add data but I don't know where and when to invoke QAbstractItemView::setIndexWidge to set my customed widget. Because I only add/modify data in my model class, but setIndexWidge is in QAbstractItemView class.
I tried to rewrite QAbstractItemView::dataChanged and QAbstractItemView::rowsInserted, in these two virtual functions, I want to invoke QAbstractItemView::setIndexWidge to set my customed widget, but these two virtual functions never invoked when I modify ABClist or invoke QAbstractItemModel::reset. Why???
Any idea when and where to invoke QAbstractItemView::setIndexWidge?
Thanks in advance.
Bookmarks