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 different background colors for rows in qtreeview
Qt 6.11 is out! See what's new in the release blog

how to set different background colors for rows in qtreeview

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 4.0k 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.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #1

    Tried to do it as follows:
    model()->setData( index, QVariant( QColor ), Qt::BackgroundRole );
    No success. Any recommendation?

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      https://www.qtcentre.org/threads/61716-Set-the-color-of-a-row-in-a-qtreeview
      found one here. Kind of messy.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        n-2204
        wrote on last edited by
        #3

        check this is code to set same color in column in Qtableview ..might be helpful

        QAbstractItemModel* table1 = ui.tableView_1->model();
        for (int row = 0; row < table1->rowCount(); ++row) {
            for (int column : {0, 1, 2)
                table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
        
        JoeCFDJ 1 Reply Last reply
        1
        • N n-2204

          check this is code to set same color in column in Qtableview ..might be helpful

          QAbstractItemModel* table1 = ui.tableView_1->model();
          for (int row = 0; row < table1->rowCount(); ++row) {
              for (int column : {0, 1, 2)
                  table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole);
          
          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @n-2204 said in how to set different background colors for rows in qtreeview:

          QBrush(QColor(240, 248, 255))

          Thanks. I have similar code for table widget and it works. But this does not work for qtreeview somehow.

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

            Hi
            But what model type is it?
            If a custom model it will be handled by the data function when asked for the BackgroundRole.

            JoeCFDJ 1 Reply Last reply
            1
            • mrjjM mrjj

              Hi
              But what model type is it?
              If a custom model it will be handled by the data function when asked for the BackgroundRole.

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @mrjj QFileSystemModel of qt 5.15.2

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

                @JoeCFD said in how to set different background colors for rows in qtreeview:

                QFileSystemModel of qt 5.15.2

                So why should this model handle setData() with Qt::BackgroundRole? btw: setData() has a return value...

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

                JoeCFDJ 1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  I use alternating color feature which solves my problem while I need only two colors. But that setData does not work is weird.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @JoeCFD said in how to set different background colors for rows in qtreeview:

                    QFileSystemModel of qt 5.15.2

                    So why should this model handle setData() with Qt::BackgroundRole? btw: setData() has a return value...

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher That is how it works. If qtreewidget is used, it has setData. But qtreeview does not have setData and its properties are manipulated with model.

                    1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @mrjj QFileSystemModel of qt 5.15.2

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      @JoeCFD said in how to set different background colors for rows in qtreeview:

                      QFileSystemModel

                      This model only handles Qt::EditRole.

                      You can use RoleMaskProxyModel from this library to support other roles. See this example

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      JoeCFDJ 1 Reply Last reply
                      3
                      • VRoninV VRonin

                        @JoeCFD said in how to set different background colors for rows in qtreeview:

                        QFileSystemModel

                        This model only handles Qt::EditRole.

                        You can use RoleMaskProxyModel from this library to support other roles. See this example

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

                        @VRonin Excellent! Thanks.

                        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