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 set the color of the selected QTreeWidgetItem?
Forum Updated to NodeBB v4.3 + New Features

How to set the color of the selected QTreeWidgetItem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 13.2k 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.
  • O Offline
    O Offline
    opengpu2
    wrote on 20 Feb 2017, 05:17 last edited by
    #3

    BackgroundRole? i think this is not what i need.
    cause i just want to change the background color of the Selected WidgetItem. or remove the background color of the Selected WidgetItem

    1 Reply Last reply
    0
    • O opengpu2
      20 Feb 2017, 03:20

      How to set the color of the selected QTreeWidgetItem?
      the default selected background color of QTreeWidgetItem is dark-blue, i want to remove this background color and just set a red rectangle for the selected item.
      thank you!

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 20 Feb 2017, 05:28 last edited by
      #4

      @opengpu2
      Hi,

      I would suggest looking into StyleSheets

      with them its easy to accomplish what you want in your case:

      the default selected background color of QTreeWidgetItem is dark-blue, i want to remove this background color and just set a red rectangle for the selected item.
      thank you !

      listWidget->setStyleSheet("QListView::item:selected {background : transparent; border: solid 2px red;}");
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • O Offline
        O Offline
        opengpu2
        wrote on 20 Feb 2017, 06:08 last edited by
        #5

        thank you very much!

        J 1 Reply Last reply 20 Feb 2017, 06:11
        0
        • O opengpu2
          20 Feb 2017, 06:08

          thank you very much!

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 20 Feb 2017, 06:11 last edited by
          #6

          @opengpu2

          just noticed a typo in my code

          background:
          

          ought to be

          background-color:
          

          but you should notice that yourself when you read through the docu :)


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          O 1 Reply Last reply 22 Feb 2017, 04:11
          0
          • O Offline
            O Offline
            opengpu2
            wrote on 20 Feb 2017, 06:17 last edited by
            #7

            ok i got it, thanks very much

            1 Reply Last reply
            0
            • J J.Hilk
              20 Feb 2017, 06:11

              @opengpu2

              just noticed a typo in my code

              background:
              

              ought to be

              background-color:
              

              but you should notice that yourself when you read through the docu :)

              O Offline
              O Offline
              opengpu2
              wrote on 22 Feb 2017, 04:11 last edited by
              #8

              @J.Hilk
              i tried, but it is white background...and the clicked coloum background is grey...

              D 1 Reply Last reply 22 Feb 2017, 12:23
              0
              • R Offline
                R Offline
                Ratzz
                wrote on 22 Feb 2017, 05:20 last edited by
                #9

                @opengpu2
                You can try QBrush as shown here .

                --Alles ist gut.

                1 Reply Last reply
                1
                • O opengpu2
                  22 Feb 2017, 04:11

                  @J.Hilk
                  i tried, but it is white background...and the clicked coloum background is grey...

                  D Offline
                  D Offline
                  Devopia53
                  wrote on 22 Feb 2017, 12:23 last edited by Devopia53
                  #10

                  @opengpu2

                   QListView {
                          show-decoration-selected: 1;
                    }
                   QListView::item:selected {
                        	border: 1px solid red;
                  	color: black;
                    }
                   QListView::item:selected:!active {
                        	background: transparent;
                    }
                   QListView::item:selected:active {
                       	background: transparent;
                    }
                   QListView::item:hover {
                          border: 1px solid #FF8888;
                    }
                  

                  If you need more subcontrols, add style. Or vice versa ...

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    opengpu2
                    wrote on 23 Feb 2017, 03:49 last edited by
                    #11

                    thank you very much
                    it does work!
                    but my problem is more complicated.
                    i have 2 column in a QTreeWidget, the 1st column is text, the 2nd column i set different color by setBackgroundColor for different rows.
                    and the problem is the default selected item is all blue...
                    now i just want the selected item has a border, and the 1st column has default white background and black text, the 2nd column still has its own background color which i set for different rows.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      opengpu2
                      wrote on 24 Feb 2017, 09:14 last edited by
                      #12

                      anyone can help? thx

                      J 1 Reply Last reply 24 Feb 2017, 09:49
                      0
                      • O opengpu2
                        24 Feb 2017, 09:14

                        anyone can help? thx

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 24 Feb 2017, 09:49 last edited by
                        #13

                        @opengpu2

                        You can always disable the default focus of your listwidget

                        widget->setFocusPolicy(Qt::NoFocus);
                        

                        And "create your own 'focusHanlder' "

                        connect(widget, &QListWidget::clicked, [=](QModelIndex index){
                        //Do Custom Stuff with clicked item
                        });
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0

                        12/13

                        24 Feb 2017, 09:14

                        • Login

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