Qt World Summit: Register Today!

QTreeView delegate with background covering only text - howto

  • Hello forum,
    I've the classic QTreeView, QStandardItemModel pair. QTreeView has one column.
    I'd like that when the user selects an item the background covers only the text, not the entire row,. The result I would like to obtain is shown in the figure. Any kind of suggestion is strongly appreciated, starting from a straightforward one (a qss for example) to a detail within the implementation of a custom delegate. For the moment I've not been able to obtain any result.
    Thanks in advance
    0_1555065059530_highlight only text.PNG

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show what you are actually getting ?

    Unless you changed the selection mode, it should only select a cell.

  • SGaist thank you
    see attachment
    As you can see the blue selection box extends up to the end of the column, also with a surrounding box. I would like a shorter selection box (with or without surrounding rectangle) starting from the beginning of the text, and ending at the end of the text.

  • Lifetime Qt Champion

    Then a custom QStyleItemDelegate is what you want. Re-implement the paint method to draw the selection the way you want it.

Log in to reply