PDA

View Full Version : QStyledItemDelegate in QTreeView



PinTxO
24th December 2016, 12:10
Hi guys.

I have a QTreeView with only one column. The data is hierarchical so there are items that have children and others that do not. Each item is viewed with a subclass of QStyledItemDelegate. This subclass basically consists of a checkbox editable by the user, a number to the right of the checkbox to indicate the number of children the item has, one image to the right of the number and another image to the right of the other image.

The checkbox of each node draws with the QStyle::drawControl method and QStyle::CE_CheckBox as ControlElement in the paint method of my subclass. In the createEditor method of my subclass I create a QCheckBox to be able to change the value. The problem arises in this point. I want to change the value when the user clicks only on the square of the checkbox. At this time, the value changes if the user clicks on the text of the checkbox, on the icons, on the number the children, even the value changes when the user clicks to the left of the node, i mean, in the indentation(tab) of the son with respect to his father. This last behavior I do not understand, it is as if the delegate were the whole row, instead of where you start to paint each node.

Any ideas?

NameRakes
24th December 2016, 16:49
I have a similar situation in my app, and I think there are two solutions:

1. Through a context menu in the tree view of the items, which will prompt an Edit action, produce a widget that contains checkbox, number of children, and icons. Then provide a delegate for each of these entities. This is the solution I am planning on implementing for my case because my data item is complex.

2. Each data item can return a QVariant depending on the role (Qt::DisplayRole, Qt::CheckStateRole, etc.). Then QStyledItemDelegate or its subclass will work with the item and respond to this role. This solution might work for you as long as the Qt::Roles suffice for your data item, and your delegate is able to respond to these roles.

PinTxO
2nd January 2017, 19:41
Hi.

I do not understand either of the two solutions you tell me. Maybe I have not explained myself well. The problem is to be able to detect in which zone of the delegate the user has clicked, so that I can control the actions to be performed since I am only interested in clicking on the square of the checkbox, not in the rest.