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. How to NOT change text color of selected QTreeWidgetItem
Forum Updated to NodeBB v4.3 + New Features

How to NOT change text color of selected QTreeWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 894 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.
  • B Offline
    B Offline
    Beatler
    wrote on last edited by
    #1

    I am using QTreeWidget. Each QTreeWidgetItem in this QTreeWidget has its own status, I wanted to display this status by adding to all items second column with symbol ⬤, and after that, just change the text color for this column for particular element, when its status will change. But in my QTreeWidget, items are selectable. When QTreeWidgetItem is selected, it changes background color as specified in my stylesheet: QTreeWidget::item:selected { background-color: red; }. But this is also changing the text color of the selected item. This seems strange because by default selected items changes only it's background color, but when you provide background-color with stylesheet it also changes text color.

    Is it possible to leave the text color unchanged in this situation?

    A J 2 Replies Last reply
    0
    • B Beatler

      I am using QTreeWidget. Each QTreeWidgetItem in this QTreeWidget has its own status, I wanted to display this status by adding to all items second column with symbol ⬤, and after that, just change the text color for this column for particular element, when its status will change. But in my QTreeWidget, items are selectable. When QTreeWidgetItem is selected, it changes background color as specified in my stylesheet: QTreeWidget::item:selected { background-color: red; }. But this is also changing the text color of the selected item. This seems strange because by default selected items changes only it's background color, but when you provide background-color with stylesheet it also changes text color.

      Is it possible to leave the text color unchanged in this situation?

      A Offline
      A Offline
      aliks-os
      wrote on last edited by
      #2

      @Beatler said in How to NOT change text color of selected QTreeWidgetItem:

      QTreeWidget::item:selected

      QTreeWidget::item:selected, QTreeWidget::item:selected:active, QTreeWidget::item:selected:!active {
      border:none;
      background-color:rgba(255,255,255,0);
      color:rgba(10,10,255,0);
      }

      B 1 Reply Last reply
      0
      • A aliks-os

        @Beatler said in How to NOT change text color of selected QTreeWidgetItem:

        QTreeWidget::item:selected

        QTreeWidget::item:selected, QTreeWidget::item:selected:active, QTreeWidget::item:selected:!active {
        border:none;
        background-color:rgba(255,255,255,0);
        color:rgba(10,10,255,0);
        }

        B Offline
        B Offline
        Beatler
        wrote on last edited by Beatler
        #3

        @aliks-os Hi, thank you for your answer, but your code makes the selected item invisible. This is not what I want to do.

        1 Reply Last reply
        0
        • B Beatler

          I am using QTreeWidget. Each QTreeWidgetItem in this QTreeWidget has its own status, I wanted to display this status by adding to all items second column with symbol ⬤, and after that, just change the text color for this column for particular element, when its status will change. But in my QTreeWidget, items are selectable. When QTreeWidgetItem is selected, it changes background color as specified in my stylesheet: QTreeWidget::item:selected { background-color: red; }. But this is also changing the text color of the selected item. This seems strange because by default selected items changes only it's background color, but when you provide background-color with stylesheet it also changes text color.

          Is it possible to leave the text color unchanged in this situation?

          J Offline
          J Offline
          Jota
          wrote on last edited by
          #4

          @Beatler Hi, have you solved this proplem, I met same proplem

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            This is a "feature" of the default delegate. It's easily solved:
            just add this class

            class HighlightTextDelegate : public QStyledItemDelegate{
                Q_OBJECT
            #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
                Q_DISABLE_COPY_MOVE(HighlightTextDelegate)
            #else
                Q_DISABLE_COPY(HighlightTextDelegate)
            #endif
            public:
                using QStyledItemDelegate::QStyledItemDelegate;
            protected:
                void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                {
                    QStyledItemDelegate::initStyleOption(option, index);
                    const QVariant foregroundData = index.data(Qt::ForegroundRole);
                    if (foregroundData.canConvert<QBrush>())
                        option->palette.setBrush(QPalette::HighlightedText, qvariant_cast<QBrush>(foregroundData));
                }
            };
            

            then call treeWidget->setItemDelegate(new HighlightTextDelegate(treeWidget));

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

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