<?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 save and restore QPen and QBrush states?]]></title><description><![CDATA[<p dir="auto">Hi,<br />
Here is my code to explode a slice when hovering the mouse over and then restore the slice to its original position. The problem is when the mouse moves away, the colour of the slice is blank(white). Why?</p>
<pre><code>for (auto* slice : series-&gt;slices()) {
            slice-&gt;setLabelVisible();
            QPen defaultPen = slice-&gt;pen();
            QBrush defaultBrush = slice-&gt;brush();
            connect(slice, &amp;QPieSlice::hovered, [slice, defaultBrush, defaultPen](bool hovered){
                if (hovered)
                {
                    slice-&gt;setPen(QPen(Qt::darkGreen, 2));
                    slice-&gt;setBrush(QBrush(Qt::green));
                    slice-&gt;setExploded(hovered);
                }
                else
                {
                    slice-&gt;setPen(defaultPen);
                    slice-&gt;setBrush(defaultBrush);
                    slice-&gt;setExploded(hovered);
                }

            });
        }
</code></pre>
<p dir="auto">Thank you very much.</p>
]]></description><link>https://forum.qt.io/topic/157249/how-to-save-and-restore-qpen-and-qbrush-states</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 01:29:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157249.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Jun 2024 02:40:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 22:54:33 GMT]]></title><description><![CDATA[<p dir="auto">Thank you Pl45m4.<br />
The key to understanding how the colour works is "The series don't have any color by default and they are designed by the chart/chartview and its applied theme.". It took me a good time to understand what it meant. Now I put the for loop after the chart declaration and I can get the chart theme colour with slice-&gt;brush().</p>
]]></description><link>https://forum.qt.io/post/802674</link><guid isPermaLink="true">https://forum.qt.io/post/802674</guid><dc:creator><![CDATA[ntos]]></dc:creator><pubDate>Fri, 14 Jun 2024 22:54:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 11:39:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ntos">@<bdi>ntos</bdi></a> said in <a href="/post/802611">How to save and restore QPen and QBrush states?</a>:</p>
<blockquote>
<p dir="auto">Why was the result QColor(ARGB 1, 0, 0, 0)?</p>
</blockquote>
<p dir="auto">As I've said above, because you don't have access to the color in your <code>for</code> loop.<br />
The series don't have any color by default and they are designed by the chart/chartview and its applied theme.<br />
The default theme (<code>0</code> or "<code>QChart::ChartThemeLight</code>") is what you see when you don't set any color.<br />
<img src="https://ddgobkiprc33d.cloudfront.net/bd188051-8bba-4a55-9851-f96b2c6a663e.png" alt="ChartThemeTest.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Without making any further specifications, the slices' "own" brush is still the default one, which is <code>( 1, 0, 0, 0)</code> (= 255, 0, 0, 0) in ARGB.<br />
And (0, 0, 0) in RGB is... surprise... white.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ntos">@<bdi>ntos</bdi></a> said in <a href="/post/802613">How to save and restore QPen and QBrush states?</a>:</p>
<blockquote>
<p dir="auto">Here is the whole code:</p>
</blockquote>
<p dir="auto">Great, then mark the topic as resolved please.</p>
]]></description><link>https://forum.qt.io/post/802630</link><guid isPermaLink="true">https://forum.qt.io/post/802630</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 14 Jun 2024 11:39:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 07:35:14 GMT]]></title><description><![CDATA[<p dir="auto">Here is the whole code:</p>
<pre><code>MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui-&gt;setupUi(this);

        QPieSeries* series = new QPieSeries();
        series-&gt;append("Vegetables", 0.4);
        series-&gt;append("Beans", 0.2);
        series-&gt;append("Fruit", 0.15);
        series-&gt;append("Seeds/Nuts", 0.1);
        series-&gt;append("Whole grains", 0.15);

        QPieSlice* slice0 = series-&gt;slices().at(0);
        slice0-&gt;setPen(QPen(Qt::darkMagenta, 2));
        slice0-&gt;setBrush(QBrush(Qt::magenta));
        QPieSlice* slice1 = series-&gt;slices().at(1);

        // set the pen to draw the border of the slice
        slice1-&gt;setPen(QPen(Qt::darkGreen, 2));
        // set the brush to brush the whole slice.
        slice1-&gt;setBrush(QBrush(Qt::green));
        QPieSlice* slice2 = series-&gt;slices().at(2);
        slice2-&gt;setPen(QPen(Qt::darkBlue, 2));
        slice2-&gt;setBrush(QBrush(Qt::blue));
        QPieSlice* slice3 = series-&gt;slices().at(3);
        slice3-&gt;setPen(QPen(Qt::darkYellow, 2));
        slice3-&gt;setBrush(QBrush(Qt::yellow));
        QPieSlice* slice4 = series-&gt;slices().at(4);
        slice4-&gt;setPen(QPen(Qt::darkCyan, 2));
        slice4-&gt;setBrush(QBrush(Qt::cyan));

        // Calculate the total value of the series
        qreal total = 0;
        for (const auto* slice : series-&gt;slices()) {
            total += slice-&gt;value();
        }



        for (auto* slice : series-&gt;slices()) {
            // Calculate percentage
            double percentage = (slice-&gt;value() / total) * 100;
            QBrush defaultBrush = slice-&gt;brush();
            QPen defaultPen = slice-&gt;pen();

           
            slice-&gt;setLabel(QString("%1 (%2%)").arg(slice-&gt;label()).arg(percentage, 1, 'f',1)); 
            slice-&gt;setLabelVisible();
            slice-&gt;setLabelPosition(QPieSlice::LabelInsideNormal);

            connect(slice, &amp;QPieSlice::hovered, [slice, defaultBrush, defaultPen](bool hovered){
                if (hovered)
                {
                    slice-&gt;setPen(QPen(Qt::darkGreen, 2));
                    slice-&gt;setBrush(QBrush(Qt::green));
                    slice-&gt;setExploded(hovered);
                }
                else
                {
                    slice-&gt;setPen(defaultPen);
                    slice-&gt;setBrush(defaultBrush);
                    slice-&gt;setExploded(hovered);
                }

            });
        }
        QChart* chart = new QChart();
        chart-&gt;addSeries(series);
        chart-&gt;setTitle("What Derek ate this week");
        chart-&gt;legend()-&gt;hide();
        chart-&gt;setAnimationOptions(QChart::AllAnimations);

        QChartView* chartView = new QChartView(chart);
        chartView-&gt;setRenderHint(QPainter::Antialiasing);
        //set chartView as the main central widget
        this-&gt;setCentralWidget(chartView);


}
</code></pre>
]]></description><link>https://forum.qt.io/post/802613</link><guid isPermaLink="true">https://forum.qt.io/post/802613</guid><dc:creator><![CDATA[ntos]]></dc:creator><pubDate>Fri, 14 Jun 2024 07:35:14 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 07:24:43 GMT]]></title><description><![CDATA[<p dir="auto">Thank you.</p>
<pre><code>QPieSlice* slice2 = series-&gt;slices().at(2);
QBrush brush = slice2-&gt;brush();
qDebug() &lt;&lt; "Default Brush is: " &lt;&lt; pen;
for {  to change brush when mouse hovers over a slice and reuse brush when mouse moves away. } 
</code></pre>
<p dir="auto">console output: Default Brush is:  QBrush(QColor(ARGB 1, 0, 0, 0),NoBrush).<br />
But as you see, I stored the brush before changing the slice properties, and the result was QColor(ARGB 1, 0, 0, 0). At this point, I hadn't hovered the mouse over the chart yet. Why was the result QColor(ARGB 1, 0, 0, 0)?<br />
Further more, I didn't change any theme. This app is very simple. All it does is show a pie chart of 5 slices. I have no chance to change any theme.</p>
]]></description><link>https://forum.qt.io/post/802611</link><guid isPermaLink="true">https://forum.qt.io/post/802611</guid><dc:creator><![CDATA[ntos]]></dc:creator><pubDate>Fri, 14 Jun 2024 07:24:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 04:39:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ntos">@<bdi>ntos</bdi></a> said in <a href="/post/802597">How to save and restore QPen and QBrush states?</a>:</p>
<blockquote>
<p dir="auto">Don't know why Qt devs decided on pen()/brush() returning a light grey colour instead of a slice's default one.</p>
</blockquote>
<p dir="auto">Don't blame Qt for your wrong code :)</p>
<p dir="auto">If you add this before your connect, you will see that there is no data for <code>defaultBrush</code>.<br />
Therefore it's just white</p>
<pre><code>        qDebug() &lt;&lt; "Default Brush is: " &lt;&lt; defaultBrush;
        qDebug() &lt;&lt; "Slice label: " &lt;&lt; slicees-&gt;label();
</code></pre>
<p dir="auto">Output:</p>
<blockquote>
<p dir="auto"><code>Default Brush is:  QBrush(QColor(ARGB 1, 0, 0, 0),NoBrush)</code><br />
<code>Slice label:  "Jane"</code></p>
</blockquote>
<p dir="auto">It's even mentioned in the documentation:</p>
<blockquote>
<p dir="auto"><code>By default, the visual appearance of the slice is set by a theme, but the theme can be overridden by specifying slice properties. However, if the theme is changed after the slices are customized, all customization will be lost.</code></p>
</blockquote>
<p dir="auto">( <a href="https://doc.qt.io/qt-6/qpieslice.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qpieslice.html#details</a> )</p>
<p dir="auto">The "default" colors you see are the theme colors... the slice brush is still white.</p>
]]></description><link>https://forum.qt.io/post/802599</link><guid isPermaLink="true">https://forum.qt.io/post/802599</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 14 Jun 2024 04:39:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 04:14:17 GMT]]></title><description><![CDATA[<p dir="auto">Thank you. I have found a workaround by setting the pen and brush to a certain colour. Then slice-&gt;pen() will return that exact colour in the for loop. Don't know why Qt devs decided on pen()/brush() returning a light grey colour instead of a slice's default one.</p>
]]></description><link>https://forum.qt.io/post/802597</link><guid isPermaLink="true">https://forum.qt.io/post/802597</guid><dc:creator><![CDATA[ntos]]></dc:creator><pubDate>Fri, 14 Jun 2024 04:14:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to save and restore QPen and QBrush states? on Fri, 14 Jun 2024 04:40:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ntos">@<bdi>ntos</bdi></a> said in <a href="/post/802594">How to save and restore QPen and QBrush states?</a>:</p>
<blockquote>
<p dir="auto">The problem is when the mouse moves away, the colour of the slice is blank(white). Why?</p>
</blockquote>
<p dir="auto">Think about the scope of your local variables <code>defaultPen</code> and <code>defaultBrush</code>.<br />
You capture them, but when the signal is emitted they are "default" default (which is white, I think), because the actual data from your <code>slice</code> cannot be accessed.</p>
]]></description><link>https://forum.qt.io/post/802596</link><guid isPermaLink="true">https://forum.qt.io/post/802596</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 14 Jun 2024 04:40:27 GMT</pubDate></item></channel></rss>