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. Programmatically change the background color of a QModelIndex role

Programmatically change the background color of a QModelIndex role

Scheduled Pinned Locked Moved Solved General and Desktop
qmodelindex
7 Posts 4 Posters 7.9k 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.
  • P Offline
    P Offline
    phamtv
    wrote on 12 Sept 2012, 18:04 last edited by A Former User
    #1

    My application contains a QTreeView that displays a custom QSortFilterProxyModel class using a QFileSystemModel as its source. I want to programmatically change the background color of the QModelIndex I am working with (basically yellow, green, and red indicating in progress, pass, fail for the file I am processing). How can I programmatically change the background color for selected index of Treeview control? Keep in mind that the current view has a alternating color scheme that I would like to keep just in case I have not dealt with the file yet.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      noah
      wrote on 12 Sept 2012, 18:37 last edited by
      #2

      The standard method is to override the data method in your model and return the required color for the BackgroundRole

      @QVariant MyModel::data ( const QModelIndex & item, int role ) const
      {
      //change the background color
      if (role == Qt::BackgroundRole)
      {
      return QColor(255, 0, 0);
      }

      return QSortFilterProxyModel::data(item, role);
      }@

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phamtv
        wrote on 12 Sept 2012, 20:38 last edited by
        #3

        Thanks Noah.. However, how can I preserve the alternating row color for files (rows) I have processed?

        1 Reply Last reply
        0
        • N Offline
          N Offline
          noah
          wrote on 12 Sept 2012, 20:50 last edited by
          #4

          If you don't want to set the color for a specific row just fall through to the default. You'll have do something a bit more complicated than my code to determine the appropriate color, obviously.

          @QVariant MyModel::data ( const QModelIndex & item, int role ) const
          {
          //change the background color
          if (role == Qt::BackgroundRole)
          {
          if (item.data() = "In Progress")
          return QColor(255, 255, 0); //yellow
          else if (item.data() = "Fail")
          return QColor(255, 0, 0); //red
          else if (item.data() = "Pass")
          return QColor(0, 255, 0); //green
          //else fall through to the default alternating background color
          }

          return QSortFilterProxyModel::data(item, role);
          }@

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phamtv
            wrote on 13 Sept 2012, 14:51 last edited by
            #5

            it works! thanks Noah!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              StephanB
              wrote on 17 Jun 2016, 13:25 last edited by
              #6

              Thank you for your answers on this question. I found this topic and it has been very helpful. Would you be able to provide your code that calls this function to change the background color?

              Thanks!

              M 1 Reply Last reply 17 Jun 2016, 14:28
              1
              • S StephanB
                17 Jun 2016, 13:25

                Thank you for your answers on this question. I found this topic and it has been very helpful. Would you be able to provide your code that calls this function to change the background color?

                Thanks!

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 17 Jun 2016, 14:28 last edited by
                #7

                @StephanB
                hi and welcome
                The thread is 4 years old so the persons are most likely never to see it.

                The rest of the code belong to a custom/subclass model.
                So what call the code, it the framework / the view that use this model.

                You can read about it here:
                http://doc.qt.io/qt-5/model-view-programming.html
                https://www.youtube.com/watch?v=ytKDsJgJa4k
                http://www.informit.com/articles/article.aspx?p=1405547&seqNum=3

                1 Reply Last reply
                2

                • Login

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