PDA

View Full Version : When and where to invoke QAbstractItemView::setIndexWidge?



mallun.wang
2nd December 2009, 12:04
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:


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

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.

:p

wysota
3rd December 2009, 10:48
You may invoke the method whenever you want after a particular row/column in the model has been created. Just remember the widget will have nothing in common with your model - it will just be a widget placed in another widget that you will have to control (and synchronize with the model) all by yourself.