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.3k 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #2

    Use setdata function and pass the role as background role or text role

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    1 Reply Last reply
    7
    • O Offline
      O Offline
      opengpu2
      wrote on 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

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on 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 last edited by
          #5

          thank you very much!

          J.HilkJ 1 Reply Last reply
          0
          • O opengpu2

            thank you very much!

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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
            0
            • O Offline
              O Offline
              opengpu2
              wrote on last edited by
              #7

              ok i got it, thanks very much

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @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 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
                0
                • RatzzR Offline
                  RatzzR Offline
                  Ratzz
                  wrote on last edited by
                  #9

                  @opengpu2
                  You can try QBrush as shown here .

                  --Alles ist gut.

                  1 Reply Last reply
                  1
                  • O opengpu2

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

                    D Offline
                    D Offline
                    Devopia53
                    wrote on 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 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 last edited by
                        #12

                        anyone can help? thx

                        J.HilkJ 1 Reply Last reply
                        0
                        • O opengpu2

                          anyone can help? thx

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on 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

                          • Login

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