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. Setting background colour of qtableview
Forum Updated to NodeBB v4.3 + New Features

Setting background colour of qtableview

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

    Hi,

    I'd like to set a background colour of several lines in a qtableview instance based on an integer value stored in a custom table model. The logic being: if value on column x has a certain magnitude, then set the background to some colour. I read through a lot of posts regarding this issue, but could not figure out, how to do it. More specifically, should I use the custom table model, the paint method of the respective delegate or a (custom?) proxy model for this task or do I indeed need to program all of the above?

    Thanks in advance.

    Kind regards,

    Andreas

    JonBJ 1 Reply Last reply
    0
    • A andi456

      Hi,

      I'd like to set a background colour of several lines in a qtableview instance based on an integer value stored in a custom table model. The logic being: if value on column x has a certain magnitude, then set the background to some colour. I read through a lot of posts regarding this issue, but could not figure out, how to do it. More specifically, should I use the custom table model, the paint method of the respective delegate or a (custom?) proxy model for this task or do I indeed need to program all of the above?

      Thanks in advance.

      Kind regards,

      Andreas

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

      @andi456
      Probably QStyledItemDelegate is the best. But if you don't fancy that you could probably get away with doing it in data() for the background color role, either directly or through a proxy model.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        andi456
        wrote on last edited by andi456
        #3

        If I went the route of the QStyledItemDelegate, I would need to somehow access the respective value from the paint method via the QModelIndex right? In order to understand the internal process. What triggers the paint method of QStyledItemDelegate?

        JonBJ 1 Reply Last reply
        0
        • A andi456

          If I went the route of the QStyledItemDelegate, I would need to somehow access the respective value from the paint method via the QModelIndex right? In order to understand the internal process. What triggers the paint method of QStyledItemDelegate?

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

          @andi456
          That's fine, QStyledItemDelegate seems best.

          What triggers the paint method of QStyledItemDelegate?

          Just whenever it needs painting/re-painting!

          QStyledItemDelegate receives a QModelIndex(). That contains the row. You can find the "column x" with the desired color via siblingAtColumn() in the row.

          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