PDA

View Full Version : Highlight selected row in QTreeWidget



Eos Pengwern
27th January 2010, 15:26
Hello,

I have a QTreeWidget which I have set up with the options



treeWidget -> setSelectionMode(QAbstractItemView::SingleSelectio n);
treeWidget -> setSelectionBehavior(QAbstractItemView::SelectRows );


...and I initally set the first item in the widget to be the 'selected' one by means of:



QTreeWidgetItemIterator it(treeWidget); // Points to the first item by default
treeWidget -> setCurrentItem(*it);


Finally, I have successfully connected the treeWidget "currentItemChanged" signal to a slot, so that I can verify when the selected item changes (if I understand the documentation correctly, the 'selected item' is automatically the same as the 'current item' whenever the QAbstractItemView::SingleSelection mode is set).

Anyway, everything seems to work as expected, except that there doesn't seem to be any visual indication within the QTreeWidget of which row is selected at a given time. I would expect it to be highlighted in some way (for example, with white text on a blue background), but in fact all the entries in the widget are displayed in exactly the same way.

What am I missing, and how can I ensure that the selected item is properly highlighted?

Thank you.

wysota
28th January 2010, 09:26
Current item is not the same thing as selected item. Only the latter is highlighted, the former just gets a thin dotted border.

Eos Pengwern
28th January 2010, 12:38
Current item is not the same thing as selected item. Only the latter is highlighted, the former just gets a thin dotted border.

That's true, and indeed when I first implemented my QTreeWidget I didn't use selection at all; I had disabled it with



treeWidget -> setSelectionMode(QAbstractItemView::NoSelection);


... and everything worked just fine; I saw the thin dotted borders around the current item, but decided that this wasn't enough highlighting. That was when I replaced the above line with



treeWidget -> setSelectionMode(QAbstractItemView::SingleSelectio n);
treeWidget -> setSelectionBehavior(QAbstractItemView::SelectRows );


...expecting (on the basis of other people's screenshots) to see nice bold highlighting. Instead, the dotted borders went away and there is now nothing at all (except the behaviour of the application itself) to indicate which line is selected.

So I'm obviously missing something.

wysota
28th January 2010, 20:24
Are your items selectable at all? Did you set any flags for them?

Eos Pengwern
28th January 2010, 21:56
Aha! That was the problem :cool:

For each QTreeWidgetItem I had set Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable, but not Qt::ItemIsSelectable. Once I added that, it worked.

Thank you very much.