Is there a way to style the root item in a QTreeWidget using stylesheets?



  • I'm working on a new user interface that has - among other things - a few QTreeWidgets. To make it look like the rest of my application, I'm trying to apply a custom stylesheet to it. This works for the most part, however, I cannot figure out, how to change the formatting of the root item in the tree (the one that is typcially set in a gray bar on top).

    QTreeWidget:item 
    {
       background-color...
    }
    

    only works for the items below the root item. I'd be very grateful for any hint!

    Thanks!


  • Moderators

    @tobiSF Do you mean the header for the tree? Or the root item? The root item should be the same as all the other items. I would need to test it but it should be affected by your item stylesheet.

    Can you share a small code example in how you build your tree?



  • It's probably not the root item, it's more the column headline, like 'Folders' in this example. This is not my tree but it's close enough to illustrate what I'm doing.

    Example QTreeWidget

    The code is very simple, the tree in question is for a settings dialog and right now, there's only one entry besides the headline:

    QTreeWidget* pTree = new QTreeWidget();
    pTree->setColumnCount(1);
    pTree->addItem(QTreeWidgetItem(0, QStringList("User Information"));
    

    That's pretty much it, I don't have access to my actual code right now.


  • Moderators

    @tobiSF Ok that makes sense... So the control you want is called a QHeaderView. So you will need to style that separately from the QTreeView::item that you are changing.

    Here is an example from Qt docs:

    QHeaderView::section {
        background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                          stop:0 #616161, stop: 0.5 #505050,
                                          stop: 0.6 #434343, stop:1 #656565);
        color: white;
        padding-left: 4px;
        border: 1px solid #6c6c6c;
    }
    
    QHeaderView::section:checked
    {
        background-color: red;
    }
    
    /* style the sort indicator */
    QHeaderView::down-arrow {
        image: url(down_arrow.png);
    }
    
    QHeaderView::up-arrow {
        image: url(up_arrow.png);
    }
    


  • @ambershark thanks a lot, pointing out the right control was exactly what I needed. Do you know if there is a more comprehensive documentation of the entire QSS realm than what's on the Qt website?


  • Moderators

    @tobiSF I wish, I haven't found one. I just use the docs from Qt.. There are a few good pages that show everything. I found that header view one in the docs as I mentioned.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.