how to give a gradual color style to a QTableWidget item
-
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

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); } -
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

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); }@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? -
@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?@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.
-
@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.
@Blackzero Hi.
Two approaches:- write yourself a delegate that will adjust the colours appropriately
- 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 -
@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.
@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
QTableWidgetItemas @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)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
setDatafunction, I suggest making a new util class calledHeatMapor 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
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 :)