Results 1 to 1 of 1

Thread: QTreeWidget: How to highlight a QTreeWidgetItem?

  1. #1
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeWidget: How to highlight a QTreeWidgetItem?

    Hi,

    with Qt 5.5 on Windows 7 in a QTreeWiget I like to highlight a QTreeWidgetItem. (With "highlight" it's meant to be marked with the same colour as by clicking with the mouse on it (light blue color filled rectangle in Windows 7). But with the following code I only get an item marked with a light grey color filled rectangle:

    With Qt-Designer in a QWidget I placed a QTreeWidget (ui->treeWidget) with the settings:

    "selectionMode = SingleSelection"
    "selectionBehavior = SelectRows".

    In the constructor of the QWidget I set

    Qt Code:
    1. "ui->treeWidget->setColumnCount(4)";
    To copy to clipboard, switch view to plain text mode 
    Then added only top level "QTreeWigetItems":

    Qt Code:
    1. QTreeWidgetItem * item = new QTreeWidgetItem(ui->treeWidget);
    2. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); // Added this line later but it makes no difference
    3. item->setText(0, "Item1");
    4. item->setText(1, "Item2");
    5. ....
    6. ui->treeWidget->addTopLevelItem(item);
    To copy to clipboard, switch view to plain text mode 
    Now I like to highlight an item in ui->treeWidget. Result = First item is marked in grey color filled rectangle:

    Qt Code:
    1. ui->treeWidget->topLevelItem(0)->setSelected(true);
    To copy to clipboard, switch view to plain text mode 
    Same here if searching for an item. Result = Searched item is found but marked again with grey color filled rectangle:

    Qt Code:
    1. QString searchItem = "exampleItem";
    2. QList<QTreeWidgetItem *> itemList = ui->treeWidget->findItems(searchItem, Qt::MatchStartsWith, 4);
    3. if (itemList.size() > 0)
    4. ui->treeWidget->setCurrentItem(itemList[0]);
    To copy to clipboard, switch view to plain text mode 
    (If I mouse click on the scrollbar of ui->treeWidget the grey marked item is highlighted (with both above codes). Is this a focus problem?)

    How to highlight a QTreeWidgetItem?


    Added after 14 minutes:


    It was a focus problem.

    I suppose the QTreeWidget lost its focus because in the QWidget it is placed with many QCheckBoxes next to it. Maybe they were stealing its focus. After placing ui->treeWidget->setFocus() in the last line the item is highlighted correctly.
    Last edited by mireiner; 8th January 2016 at 04:59.

Similar Threads

  1. Moving QTreeWidgetItem Up and Down in a QTreeWidget
    By Staakman in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2013, 11:34
  2. Highlight a part of QTreeWidgetItem name
    By Alundra in forum Qt Programming
    Replies: 2
    Last Post: 5th August 2013, 16:45
  3. Customizing QTreeWidget and QTreeWidgetItem
    By mots_g in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2010, 06:53
  4. Highlight selected row in QTreeWidget
    By Eos Pengwern in forum Newbie
    Replies: 4
    Last Post: 28th January 2010, 21:56
  5. Replies: 1
    Last Post: 18th November 2008, 06:57

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.