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.
  • B Bonnie
    12 Oct 2022, 03:42

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

    T Offline
    T Offline
    tushu
    wrote on 12 Oct 2022, 06:12 last edited by
    #11

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

    B 1 Reply Last reply 12 Oct 2022, 09:33
    0
    • T tushu
      12 Oct 2022, 06:12

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

      B Offline
      B Offline
      Bonnie
      wrote on 12 Oct 2022, 09:33 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 12 Oct 2022, 11:51
      0
      • B Bonnie
        12 Oct 2022, 09:33

        @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 12 Oct 2022, 11:51 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 J B 3 Replies Last reply 12 Oct 2022, 12:59
        0
        • T tushu
          12 Oct 2022, 11:51

          @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 12 Oct 2022, 12:59 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 ?

          J J 2 Replies Last reply 12 Oct 2022, 13:03
          0
          • T tushu
            12 Oct 2022, 11:51

            @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.

            J Online
            J Online
            JoeCFD
            wrote on 12 Oct 2022, 13:01 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 12 Oct 2022, 13:18
            0
            • T tushu
              12 Oct 2022, 12:59

              @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 ?

              J Offline
              J Offline
              JonB
              wrote on 12 Oct 2022, 13:03 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 12 Oct 2022, 13:23
              0
              • T tushu
                12 Oct 2022, 12:59

                @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 ?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 12 Oct 2022, 13:04 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
                • J JoeCFD
                  12 Oct 2022, 13:01

                  @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 12 Oct 2022, 13:18 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
                  • J JonB
                    12 Oct 2022, 13:03

                    @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 12 Oct 2022, 13:23 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.

                    J 1 Reply Last reply 12 Oct 2022, 13:28
                    0
                    • T tushu
                      12 Oct 2022, 13:23

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 12 Oct 2022, 13:28 last edited by JonB 10 Dec 2022, 13:28
                      #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 12 Oct 2022, 13:38
                      0
                      • J JonB
                        12 Oct 2022, 13:28

                        @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 Offline
                        T Offline
                        tushu
                        wrote on 12 Oct 2022, 13:38 last edited by
                        #21

                        @JonB
                        What I understood : When I will click on any row, it will get selected and this method will get invoke. And then , thorugh this method , I will deselect my current selection by clearing bits and calling that method again. So in the end, there will not be any selection. So no dark blue default selection of row by Qt.

                        Sorry if my understanding is wrong.

                        J 1 Reply Last reply 12 Oct 2022, 13:50
                        0
                        • T tushu
                          12 Oct 2022, 13:38

                          @JonB
                          What I understood : When I will click on any row, it will get selected and this method will get invoke. And then , thorugh this method , I will deselect my current selection by clearing bits and calling that method again. So in the end, there will not be any selection. So no dark blue default selection of row by Qt.

                          Sorry if my understanding is wrong.

                          J Offline
                          J Offline
                          JonB
                          wrote on 12 Oct 2022, 13:50 last edited by JonB 10 Dec 2022, 13:52
                          #22

                          @tushu
                          Well that is about right. Certainly the end result should indeed be "So no dark blue default selection of row by Qt."

                          But "and calling that method again." does not happen. When the paint() is called by Qt to draw the item, on entry QStyleOptionViewItem *option contains various options directing how to draw it. Among these is a bit for whether the item is selected, and the base paint will act on that by showing the item in blue or whatever.

                          @Bonnie has given you an override for the initStyleOption(), whose job it is to process those options for the forthcoming paint. His override removes any State_Selected bit in the option flags, and only after that calls the base QStyledItemDelegate::initStyleOption(option, index). So that will simply not see that the item was selected/had the draw-as-selected bit set. So it will set up for the paint to draw as though the item is not selected.

                          Note that this does not affect that the item actually is selected as far as the rest of your or Qt code is concerned. It will stay selected (or unselected). Ut simply will not paint it to show it is selected.

                          T 1 Reply Last reply 12 Oct 2022, 14:05
                          0
                          • J JonB
                            12 Oct 2022, 13:50

                            @tushu
                            Well that is about right. Certainly the end result should indeed be "So no dark blue default selection of row by Qt."

                            But "and calling that method again." does not happen. When the paint() is called by Qt to draw the item, on entry QStyleOptionViewItem *option contains various options directing how to draw it. Among these is a bit for whether the item is selected, and the base paint will act on that by showing the item in blue or whatever.

                            @Bonnie has given you an override for the initStyleOption(), whose job it is to process those options for the forthcoming paint. His override removes any State_Selected bit in the option flags, and only after that calls the base QStyledItemDelegate::initStyleOption(option, index). So that will simply not see that the item was selected/had the draw-as-selected bit set. So it will set up for the paint to draw as though the item is not selected.

                            Note that this does not affect that the item actually is selected as far as the rest of your or Qt code is concerned. It will stay selected (or unselected). Ut simply will not paint it to show it is selected.

                            T Offline
                            T Offline
                            tushu
                            wrote on 12 Oct 2022, 14:05 last edited by
                            #23

                            @JonB
                            Now I understood. Before this also I had used this bit clearing. But did not get why was it for. Now I understood.
                            Thanks for your explanation.

                            I am feeling confident in Qt because of this forum and the good people like you all, who always helped me.
                            Thanks a lot once again. :)

                            1 Reply Last reply
                            0
                            • T tushu
                              12 Oct 2022, 11:51

                              @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.

                              B Offline
                              B Offline
                              Bonnie
                              wrote on 13 Oct 2022, 03:00 last edited by Bonnie
                              #24

                              @tushu Well, as I said, the light blue color is the focus frame, it indicates the current index of the view.
                              The simpliest way is just disable all the indexes in the row if you don't have any interactive operation upon them, so they can't be set to be the current index.
                              (If there is any interactive operation, then I can't understand why it need to be removed...)
                              Since you're using QTreeWidget, you can call QTreeWidgetItem::setDisabled.
                              Note that will make the item's text gray if no foreground color is set

                              1 Reply Last reply
                              1

                              20/24

                              12 Oct 2022, 13:28

                              • Login

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