Results 1 to 5 of 5

Thread: Highlight selected row in QTreeWidget

  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Highlight selected row in QTreeWidget

    Hello,

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

    Qt Code:
    1. treeWidget -> setSelectionMode(QAbstractItemView::SingleSelection);
    2. treeWidget -> setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QTreeWidgetItemIterator it(treeWidget); // Points to the first item by default
    2. treeWidget -> setCurrentItem(*it);
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight selected row in QTreeWidget

    Current item is not the same thing as selected item. Only the latter is highlighted, the former just gets a thin dotted border.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Highlight selected row in QTreeWidget

    Quote Originally Posted by wysota View Post
    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

    Qt Code:
    1. treeWidget -> setSelectionMode(QAbstractItemView::NoSelection);
    To copy to clipboard, switch view to plain text mode 

    ... 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

    Qt Code:
    1. treeWidget -> setSelectionMode(QAbstractItemView::SingleSelection);
    2. treeWidget -> setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 

    ...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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight selected row in QTreeWidget

    Are your items selectable at all? Did you set any flags for them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Highlight selected row in QTreeWidget

    Aha! That was the problem

    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.

Similar Threads

  1. Replies: 3
    Last Post: 17th November 2009, 23:01
  2. Replies: 1
    Last Post: 18th November 2008, 06:57
  3. Replies: 2
    Last Post: 26th January 2008, 10:20
  4. Showing a QtreeWidget branch as selected
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:56
  5. [QT4] Selected row in a QTreeWidget
    By vfernandez in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2006, 11:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.