PDA

View Full Version : QTreeView mouse hovering over custom cell selects row



flautzr
2nd April 2019, 16:51
Hi,

I am working now with QT for a year but I am new to this board. I googled this issue but it seems as i am the only only one who ran into this, therefore looking for an expert here for an explanation / a solution.

I designed several Dialogs with 2 QTreeViews. The right tree basically shows the detailed view of the selected item in the left tree.

In 2 of those Dialogs there is a 'favourite' column. The user can click on a star icon to add/remove the item to/from a favourite list. In order to change the checkbox to the before mentioned star icon, i had to insert a QCheckbox via insertWidget into the cell, because i could'nt customize the built in checkbox through css (the star never appeared -> https://stackoverflow.com/questions/52388311/custom-checkbox-in-qtreeview-doesnt-show). Now the star-Checkbox works but there is an issue that only occurs with these 'special' cells.

For every other cell that contains a checkbox i just used the built in functionality (setCheckable(true)) and they work as intended without any misbehaviour. The special cells however get selected when the mouse hovers over that, but only once and only after another line was selected before. It behaves very strange, when hovering over the favourite column it does select the row but it does not call the selectionChanged function but getSelectedIndexes yields the wrongly selected row the mouse hovered over.

Does anyone have a clue why this is happening and how to prevent it? I overrode mouseMoveEvent of the QTreeView and it actually prevented this from happening, but at the same time disabled any drag&drop on it, so thats not a real solution.

Another issue i have is that i cant center the custom checkbox in the cell. I use a custom delegate which overrides the paint function to center checkboxes and it works well on the standardItems which are set chackable, but the cutom checkbox stays fixed at the left side of the cell ( i tried centering it with css 'margin: auto;' but it didnt have an effect).

Any hint is appreciated, tell me if you need more details.

anda_skoa
3rd April 2019, 09:01
Adding widgets to item views is always tricky as they have their own event handling and painting.

I guess in this case, where you only want to change how the "checkable" state is draw, it might be better to use a custom delegate that does just that.

Cheers,
_