Important: Please read the Qt Code of Conduct -

QTreeView problem

  • For various reasons I wish to hide the expand/collapse arrows on my QTreeView derived class. So I used a stylesheet:

    QTreeView::branch {image: none;}

    This looks nice and it's still nice when I select an item in the tree. But when I move focus away from the tree the background color of the selected row changes appropriately, but the background under expand/collapse indicator does not.

    No matter what I try, I just can't get control over the the background color of that portion in the selected:!focus state.

    Suggestions would be welcome. Here's an easy way to see the problem:

    Open the "treemodelcompleter" demo.

    In mainwindow.cpp add following line after the new QTreeview (at approx line 98).

    treeView->setStyleSheet("QTreeView::branch {image: none;}");

    Run the program and select one of the Grandchildren in the tree. The hit tab to move focus and you'll see the selected row has two different background colors.

  • Lifetime Qt Champion

    im not sure im seeing it?
    win 10. Qt 5.6

  • Thanks for that.

    I think it depends on what you see when the row is selected and the QTreeView has focus. Is the entire row selected? Or just "Grandchild" and everything to the right of it.

    Here's what I get with and without focus:

    This with Linux Mint Xfce 17.3 and Qt 5.6.

  • Lifetime Qt Champion

    well, picture is done after click grandchild, press tab.

    with this
    treeView->setStyleSheet("QTreeView::branch {image: none;} QTreeView::branch:selected {background-color: yellow;}");

    I can make that part yellow so may u can cheat that way?
    (using white)

  • Thanks but it doesn't really help because I can't seem to get control of the background in the focused and not focused states. Therefore I can't keep the color consistent with the other row item(s).

    Going back to the basic image: none; style, I did make another very interesting observation. If I resize the treemodelcompleter window, the colors are great while dragging. As soon as I stop dragging, I'm back to the same problem.

    Seems to me there is something funky going on with the painting of the background in the branch:: region with no image.

    Perhaps I will poke around in the QTreeView source code when time permits although that may be in a few days to week. In the meantime, I appreciate you taking a look. Thank you!

  • Well, this solved the problem rather nicely and easily :

    void MyTreeView::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const


Log in to reply