PDA

View Full Version : QTreeView and QStandardItemModel limitations when using custom delegates



enter
4th March 2014, 19:09
10103

Hi all,

I have problem using QTreeView with delegates in a QStandardItemModel model. I used the setItemDelegateForColumn method to apply my delegate to the second column of my QTreeView but it is also applied on the "root" rows column which I don't want. It doesn't seem possible to apply a delegate on a specific number of cells.

I attached a picture to this thread showing my ongoing result. If mya briefly comment it, I would like the <arbiter> row ComboBox delegate to dissapear. What do I have to do in order to get what I want ? Implement a custom Model or a Custom view ?

Ideally I would like to implement the "Property Editor" of QtCreator but I don't know how to do this.

Thanks for your help

ChrisW67
4th March 2014, 23:23
It doesn't seem possible to apply a delegate on a specific number of cells.
The delegate is free to distinguish between cells any way it sees fit. For example, do not paint the top level cells:


// something like
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.isvalid())
return;

const QModelIndex leftmostSibling = index.sibling(index.row(), 0);
if (leftmostSibling.parent().isValid()) {
// not on top level of tree
// paint stuff
}
else {
// default behaviour
QStyledItemDelegate::paint(painter, option, index);
}
}



You can look at the source for the "Property Editor" of QtCreator, so there should not be a huge mystery.
There's also an independent implementation at http://qt-apps.org/content/show.php/QPropertyEditor?content=68684 but I cannot vouch for its completeness, robustness etc.

enter
5th March 2014, 10:53
Thanks for the tips ! That did the trick.

I also had to check parent validity in the createEditor method of my delegate class