Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic color for a selected QTreeViewItem?

Dynamic color for a selected QTreeViewItem?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #1

    Hi,
    I'm struggling to keep the color of the items of my Treeview when they are getting selected.
    I've set a color with the data role Qt::ForegroundRole.
    The color is displayed right. it is staying when I hover the item BUT it is changing to white when the item is selected....
    The only way I managed to change the color is using the following stylesheet:

    QListView::item:selected,
    QListView::item:selected:active,
    QTreeView::item:selected,
    QTreeView::item:selected:active{
        border: 1px solid #4286F5;
        background-color: #dfedfa;
        color: black;
    }
    

    The problem is that I don't want all my items black. Some should keep a different color.

    I'm using a delegate where I've overridden initStyleOption and even there if I force the option.palette to be the color I want it doesn't affect the text color when selected...

    Idem if I do this in the paint method... (I've tried what was suggested here)

    Any idea how I could do it?

    Gojir4G 1 Reply Last reply
    0
    • mbruelM mbruel

      Hi,
      I'm struggling to keep the color of the items of my Treeview when they are getting selected.
      I've set a color with the data role Qt::ForegroundRole.
      The color is displayed right. it is staying when I hover the item BUT it is changing to white when the item is selected....
      The only way I managed to change the color is using the following stylesheet:

      QListView::item:selected,
      QListView::item:selected:active,
      QTreeView::item:selected,
      QTreeView::item:selected:active{
          border: 1px solid #4286F5;
          background-color: #dfedfa;
          color: black;
      }
      

      The problem is that I don't want all my items black. Some should keep a different color.

      I'm using a delegate where I've overridden initStyleOption and even there if I force the option.palette to be the color I want it doesn't affect the text color when selected...

      Idem if I do this in the paint method... (I've tried what was suggested here)

      Any idea how I could do it?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      @mbruel said in Dynamic color for a selected QTreeViewItem?:

      I'm using a delegate where I've overridden initStyleOption and even there if I force the option.palette to be the color I want it doesn't affect the text color when selected...

      Hi,

      Which ColorRole do you use when setting palette color ? It should work using QPalette::HighlightedText

      QStyleOptionViewItem ViewOption(option);
      ViewOption.palette.setColor(QPalette::HighlightedText, desiredColor);
      

      I took this code from here: https://stackoverflow.com/questions/19362443/qtreeview-selection-clears-text-color

      1 Reply Last reply
      4
      • mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by
        #3

        Well I thought I tried...
        Then I was overriding the whole QPalette with the constructor with 9 QBrush...
        I've just test your code and it works if I remove any color from the stylesheet on QTreeView:item
        Thanks a lot!

        Here is my code:

        void TreeItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
        {
            QStyledItemDelegate::initStyleOption(option, index);
            option->textElideMode = Qt::ElideNone;
        
            TreeItem *item = static_cast<const TreeModel*>(index.model())->itemFromIndex(index);
        
            if (item)
            {
                QColor color = item->data(Qt::ForegroundRole).value<QColor>();
                option->palette.setColor(QPalette::WindowText, color);
                option->palette.setColor(QPalette::HighlightedText, color);
        
                if (item->isLargeItem())
                {
                    option->font.setBold(true);
                    option->decorationSize = QSize(24,24);
                }
                if (item->isReadOnly() )
                    option->font.setItalic(true);
            }
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved