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 `QTreeWidgetItem::setBackground()` to all of the items associated with it.

How to `QTreeWidgetItem::setBackground()` to all of the items associated with it.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.3k 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 Offline
    T Offline
    thippu
    wrote on last edited by thippu
    #1

    Hi,
    1)I have QTreeWidget and there are many QTreeWidgetItem attached to it.
    2) for each of the QTreeWidgetItem I want to set the background color, to do this I have done QTreeWidgetItem::setBackground(0,QColor(Qt::green)); , but problem is, it is changing the color only if the item gets selected. I want to set it to all. how to do it? : here the link to output the image .
    3) My final aim is to achieve this User interface: link to imaget please help me to achieve it.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What items should be set to green? You can access the children via http://doc.qt.io/qt-5/qtreewidgetitem.html#child and all top level widgets with http://doc.qt.io/qt-5/qtreewidget.html#topLevelItem

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        What items should be set to green? You can access the children via http://doc.qt.io/qt-5/qtreewidgetitem.html#child and all top level widgets with http://doc.qt.io/qt-5/qtreewidget.html#topLevelItem

        T Offline
        T Offline
        thippu
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        What items should be set to green?

        I want to set to all the sub-items of the alltoplevelItems without the selection item.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So my proposed functions are exactly the ones you need for this.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            just to make sure that you noticed
            setAlternatingRowColors(true);
            http://doc.qt.io//qt-5/qabstractitemview.html#alternatingRowColors-prop

            that color every other row.
            and you can control the colors with

            setStyleSheet("alternate-background-color: yellow;background-color: red;");

            Im not sure if you just want alternating colors or something more fancy :)

            T 2 Replies Last reply
            2
            • mrjjM mrjj

              Hi
              just to make sure that you noticed
              setAlternatingRowColors(true);
              http://doc.qt.io//qt-5/qabstractitemview.html#alternatingRowColors-prop

              that color every other row.
              and you can control the colors with

              setStyleSheet("alternate-background-color: yellow;background-color: red;");

              Im not sure if you just want alternating colors or something more fancy :)

              T Offline
              T Offline
              thippu
              wrote on last edited by
              #6

              @mrjj You are right.
              I was looking for this effect, Thank you so much.

              1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                just to make sure that you noticed
                setAlternatingRowColors(true);
                http://doc.qt.io//qt-5/qabstractitemview.html#alternatingRowColors-prop

                that color every other row.
                and you can control the colors with

                setStyleSheet("alternate-background-color: yellow;background-color: red;");

                Im not sure if you just want alternating colors or something more fancy :)

                T Offline
                T Offline
                thippu
                wrote on last edited by thippu
                #7

                @mrjj can we able to set it only to children? Or can top level be another color?

                mrjjM 1 Reply Last reply
                0
                • T thippu

                  @mrjj can we able to set it only to children? Or can top level be another color?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @thippu
                  hi
                  I dont think u can specify on which ones. its all or nothing.
                  In such case, the functions mr @Christian-Ehrlicher mentions can be used to handle your own coloring.
                  You can try set topcolor to new color after you have inserted it.
                  Im not sure if its possible to override with setBackground or if the alternate code will change it back.
                  You should test it.

                  T 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @thippu
                    hi
                    I dont think u can specify on which ones. its all or nothing.
                    In such case, the functions mr @Christian-Ehrlicher mentions can be used to handle your own coloring.
                    You can try set topcolor to new color after you have inserted it.
                    Im not sure if its possible to override with setBackground or if the alternate code will change it back.
                    You should test it.

                    T Offline
                    T Offline
                    thippu
                    wrote on last edited by
                    #9

                    @mrjj okay bro, I will try.

                    1 Reply Last reply
                    1
                    • T thippu

                      @Christian-Ehrlicher

                      What items should be set to green?

                      I want to set to all the sub-items of the alltoplevelItems without the selection item.

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

                      @thippu

                      I want to set to all the sub-items of the alltoplevelItems without the selection item.

                      I'll just throw in that assuming you can address the items with a selector (e.g. QTreeWidget > QTreeWidgetItem == top-level only, :selected == selected only), a stylesheet rule (or two) could allow you to do this in one go without iterating anything....

                      T 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @thippu

                        I want to set to all the sub-items of the alltoplevelItems without the selection item.

                        I'll just throw in that assuming you can address the items with a selector (e.g. QTreeWidget > QTreeWidgetItem == top-level only, :selected == selected only), a stylesheet rule (or two) could allow you to do this in one go without iterating anything....

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

                        @JonB Thank you. Sure I will try.

                        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