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.
Forum Updated to NodeBB v4.3 + New Features

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.6k Views 1 Watching
  • 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 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
                        • JonBJ JonB

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

                          JonBJ 1 Reply Last reply
                          0
                          • T tushu

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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #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
                            0
                            • JonBJ JonB

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

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

                                • Login

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