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 give a gradual color style to a QTableWidget item
Qt 6.11 is out! See what's new in the release blog

how to give a gradual color style to a QTableWidget item

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 741 Views 2 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.
  • B Offline
    B Offline
    Blackzero
    wrote on last edited by
    #1

    as in this title how to make a gradual color on a QTableWidget/QTableView ite, I have made it but it fails, I made it from a benchmark value for example the minimum is 0 and the maximum is 15000 if the low value is close to 0 the color changes to blue if the value is almost reaching the maximum value or more the color will change to red, can someone help my problem, this is a saple photo that power wants and my previous code

    Screenshot 2024-07-25 095007.jpg

    void TabTable::setTableData(QStringList columnHeaders, QStringList rowHeaders, QVector<QStringList> valueList)
    {
        int rowCount = rowHeaders.size();
        int columnCount = columnHeaders.size();
    
        int minValue = 0;    
        int maxValue = 15000;   
    
        ui->tableWidget->setRowCount(rowCount);
        ui->tableWidget->setColumnCount(columnCount);
    
        for (int row = 0; row < rowCount; ++row) {
            for (int col = 0; col < columnCount; ++col) {
                QString cellDataStr = valueList[row][col];
                qDebug() << cellDataStr;
                int cellValue = cellDataStr.toInt(); 
    
                double colorFraction = (double)(cellValue - minValue) / (maxValue - minValue);
    
                int red = 255 * colorFraction;
                int blue = 255 * (1 - colorFraction);
    
                red = qMin(255, red); 
                blue = qMin(255, blue); 
    
                QColor cellColor(red, 0, blue); 
                QTableWidgetItem *item = new QTableWidgetItem(cellDataStr);
                item->setBackgroundColor(cellColor);
    
                ui->tableWidget->setItem(row, col, item);
            }
        }
    
        for (int row = 0; row < rowCount; ++row) {
            QTableWidgetItem *item = new QTableWidgetItem(rowHeaders[row]);
            ui->tableWidget->setVerticalHeaderItem(row, item); 
        }
    
        for (int col = 0; col < columnCount; ++col) {
            QTableWidgetItem *item = new QTableWidgetItem(columnHeaders[col]);
            ui->tableWidget->setHorizontalHeaderItem(col, item);
        }
    
        ui->tableWidget->verticalHeader()->setVisible(true);
    }
    
    
    Pl45m4P 1 Reply Last reply
    0
    • B Blackzero

      as in this title how to make a gradual color on a QTableWidget/QTableView ite, I have made it but it fails, I made it from a benchmark value for example the minimum is 0 and the maximum is 15000 if the low value is close to 0 the color changes to blue if the value is almost reaching the maximum value or more the color will change to red, can someone help my problem, this is a saple photo that power wants and my previous code

      Screenshot 2024-07-25 095007.jpg

      void TabTable::setTableData(QStringList columnHeaders, QStringList rowHeaders, QVector<QStringList> valueList)
      {
          int rowCount = rowHeaders.size();
          int columnCount = columnHeaders.size();
      
          int minValue = 0;    
          int maxValue = 15000;   
      
          ui->tableWidget->setRowCount(rowCount);
          ui->tableWidget->setColumnCount(columnCount);
      
          for (int row = 0; row < rowCount; ++row) {
              for (int col = 0; col < columnCount; ++col) {
                  QString cellDataStr = valueList[row][col];
                  qDebug() << cellDataStr;
                  int cellValue = cellDataStr.toInt(); 
      
                  double colorFraction = (double)(cellValue - minValue) / (maxValue - minValue);
      
                  int red = 255 * colorFraction;
                  int blue = 255 * (1 - colorFraction);
      
                  red = qMin(255, red); 
                  blue = qMin(255, blue); 
      
                  QColor cellColor(red, 0, blue); 
                  QTableWidgetItem *item = new QTableWidgetItem(cellDataStr);
                  item->setBackgroundColor(cellColor);
      
                  ui->tableWidget->setItem(row, col, item);
              }
          }
      
          for (int row = 0; row < rowCount; ++row) {
              QTableWidgetItem *item = new QTableWidgetItem(rowHeaders[row]);
              ui->tableWidget->setVerticalHeaderItem(row, item); 
          }
      
          for (int col = 0; col < columnCount; ++col) {
              QTableWidgetItem *item = new QTableWidgetItem(columnHeaders[col]);
              ui->tableWidget->setHorizontalHeaderItem(col, item);
          }
      
          ui->tableWidget->verticalHeader()->setVisible(true);
      }
      
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Blackzero said in how to give a gradual color style to a QTableWidget item:

      can someone help my problem

      and what is the problem?

      What is wrong with the image?
      You are still trying to make a heat map?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      B 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Blackzero said in how to give a gradual color style to a QTableWidget item:

        can someone help my problem

        and what is the problem?

        What is wrong with the image?
        You are still trying to make a heat map?

        B Offline
        B Offline
        Blackzero
        wrote on last edited by
        #3

        @Pl45m4 yes I want to make the color on each item with a gradual color as in the picture but the problem is that the color I make does not match the image, I know that the color change takes a benchmark from the value of the item and compares the max or min value that has been determined, so how can the color adjust from the value of the item, you can see in the picture that the color adjusts from the value of the item.

        artwawA Pl45m4P 2 Replies Last reply
        0
        • B Blackzero

          @Pl45m4 yes I want to make the color on each item with a gradual color as in the picture but the problem is that the color I make does not match the image, I know that the color change takes a benchmark from the value of the item and compares the max or min value that has been determined, so how can the color adjust from the value of the item, you can see in the picture that the color adjusts from the value of the item.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @Blackzero Hi.
          Two approaches:

          1. write yourself a delegate that will adjust the colours appropriately
          2. if you use a model to supply the data (I'd assume so) you can also set Qt::ForegroundRole and Qt::BackgroundRole for particular items based on the value.

          Each of these approaches has its own merit and choosing one depends on the factors I obviously can't consider here.

          For more details please take a look here:
          https://doc.qt.io/qt-6/qstyleditemdelegate.html
          https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum
          https://doc.qt.io/qt-6/model-view-programming.html

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • B Blackzero

            @Pl45m4 yes I want to make the color on each item with a gradual color as in the picture but the problem is that the color I make does not match the image, I know that the color change takes a benchmark from the value of the item and compares the max or min value that has been determined, so how can the color adjust from the value of the item, you can see in the picture that the color adjusts from the value of the item.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Blackzero said in how to give a gradual color style to a QTableWidget item:

            problem is that the color I make does not match the image

            So your problem is that you dont know how to calculate the correct color for each value and not how to set a color to a QTableWidgetItem as @artwaw is showing?!

            If you want to map values from 0 to 15.000 (15.001 "steps"), you need to normalize your data first, since RGB takes 1byte (0-255) for each channel.
            Then, apply your desired colorization.

            You could, for example, look at OpenCV's colorMap function applyColorMap (here).
            The general "heat map" (blue to red) color map is called "JET" there.

            Here is how JET is implemented:
            (other maps have different LUTs, of course)

            • https://github.com/opencv/opencv_attic/blob/master/opencv/modules/contrib/src/colormap.cpp#L231

            If your data is normalized, you can read the matching color value from that LUT.

            Tip: Before you add that "monster" of a LUT (3x 256 floats) to your setData function, I suggest making a new util class called HeatMap or something like that and continue from there... implement mapping function, get data from map/LUT... etc.

            If I'm not mistaken I already wrote something like that in another topic of yours.

            Edit:

            OR, if you have more things like this in mind, you might consider using OCV directly and adding it to your project.

            PS:

            Instead of a static Look-Up-Table you can also interpolate the color steps yourself with a function like this

            • https://stackoverflow.com/a/20793850

            This comment is IMO way better than the accepted answer to the topic on StackO, as it's a more general solution and allows setting a 3-step gradient for your mapping (min, mid, max)... in case you don't want "blue to red over green" anymore :)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            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