<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[how to give a gradual color style to a QTableWidget item]]></title><description><![CDATA[<p dir="auto">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</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/dd64f55a-6c42-4ae5-871e-bdd3a5ec40f9.jpg" alt="Screenshot 2024-07-25 095007.jpg" class=" img-fluid img-markdown" /></p>
<pre><code>void TabTable::setTableData(QStringList columnHeaders, QStringList rowHeaders, QVector&lt;QStringList&gt; valueList)
{
    int rowCount = rowHeaders.size();
    int columnCount = columnHeaders.size();

    int minValue = 0;    
    int maxValue = 15000;   

    ui-&gt;tableWidget-&gt;setRowCount(rowCount);
    ui-&gt;tableWidget-&gt;setColumnCount(columnCount);

    for (int row = 0; row &lt; rowCount; ++row) {
        for (int col = 0; col &lt; columnCount; ++col) {
            QString cellDataStr = valueList[row][col];
            qDebug() &lt;&lt; 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-&gt;setBackgroundColor(cellColor);

            ui-&gt;tableWidget-&gt;setItem(row, col, item);
        }
    }

    for (int row = 0; row &lt; rowCount; ++row) {
        QTableWidgetItem *item = new QTableWidgetItem(rowHeaders[row]);
        ui-&gt;tableWidget-&gt;setVerticalHeaderItem(row, item); 
    }

    for (int col = 0; col &lt; columnCount; ++col) {
        QTableWidgetItem *item = new QTableWidgetItem(columnHeaders[col]);
        ui-&gt;tableWidget-&gt;setHorizontalHeaderItem(col, item);
    }

    ui-&gt;tableWidget-&gt;verticalHeader()-&gt;setVisible(true);
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/157895/how-to-give-a-gradual-color-style-to-a-qtablewidget-item</link><generator>RSS for Node</generator><lastBuildDate>Tue, 03 Nov 2026 14:07:22 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157895.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Jul 2024 02:55:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to how to give a gradual color style to a QTableWidget item on Thu, 25 Jul 2024 12:49:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/blackzero">@<bdi>Blackzero</bdi></a> said in <a href="/post/805714">how to give a gradual color style to a QTableWidget item</a>:</p>
<blockquote>
<p dir="auto">problem is that the color I make does not match the image</p>
</blockquote>
<p dir="auto">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 <code>QTableWidgetItem</code> as <a class="plugin-mentions-user plugin-mentions-a" href="/user/artwaw">@<bdi>artwaw</bdi></a> is showing?!</p>
<p dir="auto">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.<br />
Then, apply your desired colorization.</p>
<p dir="auto">You could, for example, look at OpenCV's colorMap function <code>applyColorMap</code> (<a href="https://github.com/opencv/opencv_attic/blob/master/opencv/modules/contrib/src/colormap.cpp#L507" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>).<br />
The general "heat map" (blue to red) color map is called "JET" there.</p>
<p dir="auto">Here is how JET is implemented:<br />
(other maps have different LUTs, of course)</p>
<ul>
<li><a href="https://github.com/opencv/opencv_attic/blob/master/opencv/modules/contrib/src/colormap.cpp#L231" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/opencv/opencv_attic/blob/master/opencv/modules/contrib/src/colormap.cpp#L231</a></li>
</ul>
<p dir="auto">If your data is normalized, you can read the matching color value from that LUT.</p>
<p dir="auto">Tip: Before you add that "monster" of a LUT (3x 256 floats) to your <code>setData</code> function, I suggest making a new util class called <code>HeatMap</code> or something like that and continue from there... implement mapping function, get data from map/LUT... etc.</p>
<p dir="auto">If I'm not mistaken I already wrote something like that in another topic of yours.</p>
<p dir="auto">Edit:</p>
<p dir="auto">OR, if you have more things like this in mind, you might consider using OCV directly and adding it to your project.</p>
<p dir="auto">PS:</p>
<p dir="auto">Instead of a static Look-Up-Table you can also interpolate the color steps yourself with a function like this</p>
<ul>
<li><a href="https://stackoverflow.com/a/20793850" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/a/20793850</a></li>
</ul>
<p dir="auto">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 :)</p>
]]></description><link>https://forum.qt.io/post/805758</link><guid isPermaLink="true">https://forum.qt.io/post/805758</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Thu, 25 Jul 2024 12:49:01 GMT</pubDate></item><item><title><![CDATA[Reply to how to give a gradual color style to a QTableWidget item on Thu, 25 Jul 2024 10:27:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/blackzero">@<bdi>Blackzero</bdi></a> Hi.<br />
Two approaches:</p>
<ol>
<li>write yourself a delegate that will adjust the colours appropriately</li>
<li>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.</li>
</ol>
<p dir="auto">Each of these approaches has its own merit and choosing one depends on the factors I obviously can't consider here.</p>
<p dir="auto">For more details please take a look here:<br />
<a href="https://doc.qt.io/qt-6/qstyleditemdelegate.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qstyleditemdelegate.html</a><br />
<a href="https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum</a><br />
<a href="https://doc.qt.io/qt-6/model-view-programming.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/model-view-programming.html</a></p>
]]></description><link>https://forum.qt.io/post/805748</link><guid isPermaLink="true">https://forum.qt.io/post/805748</guid><dc:creator><![CDATA[artwaw]]></dc:creator><pubDate>Thu, 25 Jul 2024 10:27:10 GMT</pubDate></item><item><title><![CDATA[Reply to how to give a gradual color style to a QTableWidget item on Thu, 25 Jul 2024 06:14:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> 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.</p>
]]></description><link>https://forum.qt.io/post/805714</link><guid isPermaLink="true">https://forum.qt.io/post/805714</guid><dc:creator><![CDATA[Blackzero]]></dc:creator><pubDate>Thu, 25 Jul 2024 06:14:37 GMT</pubDate></item><item><title><![CDATA[Reply to how to give a gradual color style to a QTableWidget item on Thu, 25 Jul 2024 05:31:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/blackzero">@<bdi>Blackzero</bdi></a> said in <a href="/post/805703">how to give a gradual color style to a QTableWidget item</a>:</p>
<blockquote>
<p dir="auto">can someone help my problem</p>
</blockquote>
<p dir="auto">and what is the problem?</p>
<p dir="auto">What is wrong with the image?<br />
You are still trying to make a heat map?</p>
]]></description><link>https://forum.qt.io/post/805710</link><guid isPermaLink="true">https://forum.qt.io/post/805710</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Thu, 25 Jul 2024 05:31:30 GMT</pubDate></item></channel></rss>