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 use style sheet in qt to not to change row color on selection.
QtWS25 Last Chance

How to use style sheet in qt to not to change row color on selection.

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 4.1k 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by tushu
    #1

    I have a QTreeWidget with some rows and columns. Rows have specific color.

    But on mouse click ( selection ) it is changing background color (which I do not want). ( Please look below screen shot)

    DT1.PNG

    ( I have clicked on the middle row and it is changing it's background color )

    So I tried following style sheet ( but it is not working )

    "QTreeWidget::item:selected:active {
          "border: 1px solid red;"
          "}";
    

    So What am I looking for ?

    1. On selection of row, it should not change its background color, but I should get some feel of selection (like border should get darken or border color should change etc )

    How to do that ? Can anyone help me in style sheet ?

    T 1 Reply Last reply
    0
    • T tushu

      I have a QTreeWidget with some rows and columns. Rows have specific color.

      But on mouse click ( selection ) it is changing background color (which I do not want). ( Please look below screen shot)

      DT1.PNG

      ( I have clicked on the middle row and it is changing it's background color )

      So I tried following style sheet ( but it is not working )

      "QTreeWidget::item:selected:active {
            "border: 1px solid red;"
            "}";
      

      So What am I looking for ?

      1. On selection of row, it should not change its background color, but I should get some feel of selection (like border should get darken or border color should change etc )

      How to do that ? Can anyone help me in style sheet ?

      T Offline
      T Offline
      tushu
      wrote on last edited by
      #2

      @mrjj @VRonin @jsulm @orgads @aha_1980 @Christian-Ehrlicher

      I am trying over this issue past 2 days, but I am not getting any help.
      So can you help me in " How to write style sheet which will not change a row color when we click on row"

      1 Reply Last reply
      0
      • sbelaS Offline
        sbelaS Offline
        sbela
        wrote on last edited by sbela
        #3

        @tushu said in How to use style sheet in qt to not to change row color on selection.:

        QTreeWidget

        Try changing this to QTreeView like in the help is suggested:
        "```
        QTreeView {
        show-decoration-selected: 1;
        }

        QTreeView::item {
        border: 1px solid #d9d9d9;
        border-top-color: transparent;
        border-bottom-color: transparent;
        }

        QTreeView::item:hover {
        background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
        border: 1px solid #bfcde4;
        }

        QTreeView::item:selected {
        border: 1px solid #567dbc;
        }

        QTreeView::item:selected:active{
        background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
        }

        QTreeView::item:selected:!active {
        background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
        }

        I would like!

        T 1 Reply Last reply
        0
        • sbelaS sbela

          @tushu said in How to use style sheet in qt to not to change row color on selection.:

          QTreeWidget

          Try changing this to QTreeView like in the help is suggested:
          "```
          QTreeView {
          show-decoration-selected: 1;
          }

          QTreeView::item {
          border: 1px solid #d9d9d9;
          border-top-color: transparent;
          border-bottom-color: transparent;
          }

          QTreeView::item:hover {
          background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
          border: 1px solid #bfcde4;
          }

          QTreeView::item:selected {
          border: 1px solid #567dbc;
          }

          QTreeView::item:selected:active{
          background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
          }

          QTreeView::item:selected:!active {
          background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
          }

          T Offline
          T Offline
          tushu
          wrote on last edited by
          #4

          @sbela
          I tried what you said. But it is still showing the same.
          On clicking row, rows color is changing into blue.

          1 Reply Last reply
          0
          • sbelaS Offline
            sbelaS Offline
            sbela
            wrote on last edited by
            #5

            Ok! You are right :)
            I have searched a bit and in src of Qt6.4.0 in: qtbase/src/widgets/styles/qcommonstyle.cpp at line: 657
            you see how the default item is drawn, therefore if you do something like this:

            QPalette pal { ui->treeView1->palette() };
            pal.setBrush(QPalette::Active, QPalette::Highlight, Qt::red);
            ui->treeView1->setPalette(pal);
            

            for me with this I have a red background and not blue. :)

            I would like!

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6
              QTreeView::item:selected{
              color: palette(text); /* use default text color, you can change to other colors you want */
              background-color: palette(base); /* use default background color, you can change to other colors you want*/
              border: 1px solid red;/* set border properties to what you want */
              }
              

              Note that will also override the colors of hovered rows when selected, if you want to set them then add something like

              QTreeView::item:selected:hover{
              background-color: lightblue;
              }
              
              T 1 Reply Last reply
              0
              • B Bonnie
                QTreeView::item:selected{
                color: palette(text); /* use default text color, you can change to other colors you want */
                background-color: palette(base); /* use default background color, you can change to other colors you want*/
                border: 1px solid red;/* set border properties to what you want */
                }
                

                Note that will also override the colors of hovered rows when selected, if you want to set them then add something like

                QTreeView::item:selected:hover{
                background-color: lightblue;
                }
                
                T Offline
                T Offline
                tushu
                wrote on last edited by
                #7

                @Bonnie Currently it is looking like this.

                An.PNG

                I did not get what is text in

                color: palette(text);
                
                B 1 Reply Last reply
                0
                • T tushu

                  @Bonnie Currently it is looking like this.

                  An.PNG

                  I did not get what is text in

                  color: palette(text);
                  
                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @tushu palette(text) means using QPalette::Text.
                  See https://doc.qt.io/qt-5/stylesheet-reference.html#brush and https://doc.qt.io/qt-5/stylesheet-reference.html#paletterole.
                  Because I don't know your full code of stylesheet or how you set colors, this just means to use the default color in normal state.
                  Sure you can use "color: black" and "background-color: green" like in usual stylesheet color properties.
                  I also notice that you still get the left empty part of blue highlighted background, this can also be changed by

                  QTreeView::branch:selected{
                  background-color: some-color;
                  }
                  

                  or just

                  QTreeView {
                  show-decoration-selected: 0;
                  }
                  
                  T 1 Reply Last reply
                  0
                  • B Bonnie

                    @tushu palette(text) means using QPalette::Text.
                    See https://doc.qt.io/qt-5/stylesheet-reference.html#brush and https://doc.qt.io/qt-5/stylesheet-reference.html#paletterole.
                    Because I don't know your full code of stylesheet or how you set colors, this just means to use the default color in normal state.
                    Sure you can use "color: black" and "background-color: green" like in usual stylesheet color properties.
                    I also notice that you still get the left empty part of blue highlighted background, this can also be changed by

                    QTreeView::branch:selected{
                    background-color: some-color;
                    }
                    

                    or just

                    QTreeView {
                    show-decoration-selected: 0;
                    }
                    
                    T Offline
                    T Offline
                    tushu
                    wrote on last edited by
                    #9

                    @Bonnie I tried what you said just and it improved my output.
                    Currently it is looking like this :

                    sbo.PNG

                    by your current suggestion that left side blue patch gone. But still I am missing my original colors. Now instead of white strip I want to my original color

                    And my current style sheet is :

                    QString GetTreeStyle()
                    {
                        QString style =
                             "QTreeView {
                                "show-decoration-selected: 0;
                                             "}"
                            "QTreeView::item:selected{
                                "color: palette(text);" 
                                "background-color: palette(base);" 
                                "border: 1px solid red;"
                                "}";
                      return style;
                    }
                    
                    B 1 Reply Last reply
                    0
                    • T tushu

                      @Bonnie I tried what you said just and it improved my output.
                      Currently it is looking like this :

                      sbo.PNG

                      by your current suggestion that left side blue patch gone. But still I am missing my original colors. Now instead of white strip I want to my original color

                      And my current style sheet is :

                      QString GetTreeStyle()
                      {
                          QString style =
                               "QTreeView {
                                  "show-decoration-selected: 0;
                                               "}"
                              "QTreeView::item:selected{
                                  "color: palette(text);" 
                                  "background-color: palette(base);" 
                                  "border: 1px solid red;"
                                  "}";
                        return style;
                      }
                      
                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

                      @tushu How do you set your current colors? (Those green and yellow ones)

                      T 1 Reply Last reply
                      0
                      • B Bonnie

                        @tushu How do you set your current colors? (Those green and yellow ones)

                        T Offline
                        T Offline
                        tushu
                        wrote on last edited by
                        #11

                        @Bonnie I am setting those color through
                        setBackgroundColor() of QTreeWidgetItem.

                        B 1 Reply Last reply
                        0
                        • T tushu

                          @Bonnie I am setting those color through
                          setBackgroundColor() of QTreeWidgetItem.

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #12

                          @tushu Sadly stylesheet can't get the color set by setBackgroundColor.
                          In this situation I would suggest using a custom item delegate.

                          1. remove stylesheet contents but keep "show-decoration-selected: 0"

                          2. subclass QStyledItemDelegate and reimplement initStyleOption

                          void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                          {
                              option->state &= ~QStyle::State_Selected;  //make sure no item is flaged as selected when drawing rows
                              QStyledItemDelegate::initStyleOption(option, index);
                          }
                          
                          1. create a CustomItemDelegate object and set it to the view by setItemDelegate

                          But by this method you cannot set border for a selected row anymore.
                          By default there's a focus frame for the focused item, you may call setAllColumnsShowFocus(true) to make it cover the whole row to show the "selected row".

                          T 1 Reply Last reply
                          0
                          • B Bonnie

                            @tushu Sadly stylesheet can't get the color set by setBackgroundColor.
                            In this situation I would suggest using a custom item delegate.

                            1. remove stylesheet contents but keep "show-decoration-selected: 0"

                            2. subclass QStyledItemDelegate and reimplement initStyleOption

                            void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                            {
                                option->state &= ~QStyle::State_Selected;  //make sure no item is flaged as selected when drawing rows
                                QStyledItemDelegate::initStyleOption(option, index);
                            }
                            
                            1. create a CustomItemDelegate object and set it to the view by setItemDelegate

                            But by this method you cannot set border for a selected row anymore.
                            By default there's a focus frame for the focused item, you may call setAllColumnsShowFocus(true) to make it cover the whole row to show the "selected row".

                            T Offline
                            T Offline
                            tushu
                            wrote on last edited by
                            #13

                            @Bonnie Perfect solution. It worked perfectly.

                            But I got suggestion over the output.
                            Current output is :

                            sbela.PNG

                            Clicked row ( add_7 ) is originally without color ( white ) . But after click, it slightly becoming bluish. Can we remove that light blue color ?

                            sela11.PNG

                            2nd row is clicked.

                            But when I click on a coloured row, it looks fine.

                            Is there any way to remove that light blue color when I click on non coloured row ?

                            And many thanks for your efforts and perfect Solution.

                            T JoeCFDJ B 3 Replies Last reply
                            0
                            • T tushu

                              @Bonnie Perfect solution. It worked perfectly.

                              But I got suggestion over the output.
                              Current output is :

                              sbela.PNG

                              Clicked row ( add_7 ) is originally without color ( white ) . But after click, it slightly becoming bluish. Can we remove that light blue color ?

                              sela11.PNG

                              2nd row is clicked.

                              But when I click on a coloured row, it looks fine.

                              Is there any way to remove that light blue color when I click on non coloured row ?

                              And many thanks for your efforts and perfect Solution.

                              T Offline
                              T Offline
                              tushu
                              wrote on last edited by
                              #14

                              @Bonnie

                              One more favor.
                              You have suggested following code. BUt I am not understanding what are you doing exactly.

                              void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                              {
                                  option->state &= ~QStyle::State_Selected;  //make sure no item is flaged as selected when drawing rows
                                  QStyledItemDelegate::initStyleOption(option, index);
                              }
                              

                              In 2nd line , you are calling the initStyleOption() again.
                              But what are you doing in 1st line ?
                              Can you explain that ?

                              JonBJ jsulmJ 2 Replies Last reply
                              0
                              • T tushu

                                @Bonnie Perfect solution. It worked perfectly.

                                But I got suggestion over the output.
                                Current output is :

                                sbela.PNG

                                Clicked row ( add_7 ) is originally without color ( white ) . But after click, it slightly becoming bluish. Can we remove that light blue color ?

                                sela11.PNG

                                2nd row is clicked.

                                But when I click on a coloured row, it looks fine.

                                Is there any way to remove that light blue color when I click on non coloured row ?

                                And many thanks for your efforts and perfect Solution.

                                JoeCFDJ Offline
                                JoeCFDJ Offline
                                JoeCFD
                                wrote on last edited by
                                #15

                                @tushu light blue color may be caused by focus. Try to set no focus to each item to see if the color is gone.

                                T 1 Reply Last reply
                                0
                                • T tushu

                                  @Bonnie

                                  One more favor.
                                  You have suggested following code. BUt I am not understanding what are you doing exactly.

                                  void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                                  {
                                      option->state &= ~QStyle::State_Selected;  //make sure no item is flaged as selected when drawing rows
                                      QStyledItemDelegate::initStyleOption(option, index);
                                  }
                                  

                                  In 2nd line , you are calling the initStyleOption() again.
                                  But what are you doing in 1st line ?
                                  Can you explain that ?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @tushu said in How to use style sheet in qt to not to change row color on selection.:

                                  But what are you doing in 1st line ?

                                  option->state &= ~QStyle::State_Selected; //make sure no item is flaged as selected when drawing rows

                                  Switches off/clears the QStyle::State_Selected bit in option->state, leaving all other bits untouched.

                                  T 1 Reply Last reply
                                  0
                                  • T tushu

                                    @Bonnie

                                    One more favor.
                                    You have suggested following code. BUt I am not understanding what are you doing exactly.

                                    void CustomItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                                    {
                                        option->state &= ~QStyle::State_Selected;  //make sure no item is flaged as selected when drawing rows
                                        QStyledItemDelegate::initStyleOption(option, index);
                                    }
                                    

                                    In 2nd line , you are calling the initStyleOption() again.
                                    But what are you doing in 1st line ?
                                    Can you explain that ?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @tushu said in How to use style sheet in qt to not to change row color on selection.:

                                    But what are you doing in 1st line ?

                                    https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • JoeCFDJ JoeCFD

                                      @tushu light blue color may be caused by focus. Try to set no focus to each item to see if the color is gone.

                                      T Offline
                                      T Offline
                                      tushu
                                      wrote on last edited by
                                      #18

                                      @JoeCFD
                                      To get focus I have added following line.

                                      setAllColumnsShowFocus(true);
                                      

                                      If I remove that line, then by default focus will be given to a particular cell ( column) where I have clicked with same light blue colour.

                                      Moreover, If I remove it completely, then I will miss a feel of row selection. I want row selection feeling also.

                                      1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @tushu said in How to use style sheet in qt to not to change row color on selection.:

                                        But what are you doing in 1st line ?

                                        option->state &= ~QStyle::State_Selected; //make sure no item is flaged as selected when drawing rows

                                        Switches off/clears the QStyle::State_Selected bit in option->state, leaving all other bits untouched.

                                        T Offline
                                        T Offline
                                        tushu
                                        wrote on last edited by
                                        #19

                                        @JonB Thanks for your help.
                                        Sorry but still I am confused.
                                        I did not get the purpose behind this bits clearing.

                                        JonBJ 1 Reply Last reply
                                        0
                                        • T tushu

                                          @JonB Thanks for your help.
                                          Sorry but still I am confused.
                                          I did not get the purpose behind this bits clearing.

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #20

                                          @tushu
                                          What do you not understand? @Bonnie wrote a comment against the line for you:

                                          //make sure no item is flaged as selected when drawing rows

                                          Isn't that what you have been asking for?

                                          T 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