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. Background color of row in QAbstractItemModel

Background color of row in QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 5.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.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by
    #1

    Hello,

    A quick question.
    Is there any way to get the background color of each row in QAbstractItemModel?
    Now I'm implementing QTableView and trying to get current background color in data()(eg.BackgroundRole) in the model. I want to change the background and then, resume it later.
    How can I achieve that?

    Many thanks in advance for your help.

    Regards,
    Sat

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You should implement data() function. Inside the data function, return the appropriate color for the background role.

      Sample inside the data function.

      case Qt::BackgroundRole: 
          QColor col1(0,255,255);
          QVariant var2 = col1;
          return var2;
      

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

      S 1 Reply Last reply
      1
      • dheerendraD dheerendra

        You should implement data() function. Inside the data function, return the appropriate color for the background role.

        Sample inside the data function.

        case Qt::BackgroundRole: 
            QColor col1(0,255,255);
            QVariant var2 = col1;
            return var2;
        
        S Offline
        S Offline
        ShinSat
        wrote on last edited by
        #3

        Thanks for an update, @dheerendra .
        I may be misundersting something. Is it possible to READ the current background color of the corresponding row(line)?

        Sat

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          You can store the background color in the model with Qt::background role. Once u r inside the data(), you can read and return the background color.
          Hope this is what you would like to achieve.

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

          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