Search code examples
qtstylesheetqtreeview

Qt stylesheets with qtreeview::item only applies to child items?


I want to style the items of a QTreeView, namely increase their top/bottom padding a bit. This is my snippet:

QTreeView::item
{
    padding-top:    8px;
    padding-bottom: 8px;
    color: red;
}

Unfortunately, this only applies the padding to child items, ie. all top-level items have exactly the same padding as before. I added the "color: red;" just for testing purposes, but the red text color, on the other hand, applies to all items no matter if top-level or child.

Is there some special qualifier that I am missing? I tried with :active and :has-sibling, but the results were the same for each case.

Screenshot

I am using Qt 4.6.3. I also tried with 4.8, but results are the same.


Solution

  • The problem was with a QStyledItemDelegate subclass I was using on the view. The delegate was only meant to draw horizontal lines for items, and I did not reimplement the sizehint function, but upon further inspection, it appeared that the subclass was returning the wrong sizehint.

    I therefore went with DanielCastro's advice and just used the virtual sizehint function in the delegate subclass.