<?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[Custom QStyle for QLabel: Issue with Double Text Rendering]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I'm working on creating a custom QStyle for a QLabel without relying on style sheets.<br />
I've successfully implemented a similar approach for customizing a button's style, and it works as expected.<br />
However, when applying it to the label, I'm encountering an issue where the label renders two overlapping instances of the text.</p>
<p dir="auto">The issue I'm facing is that the label renders two instances of the text.<br />
The first one is painted according to the custom requirements from the code, but the default text rendered by QLabel remains visible and is not removed.<br />
I need to prevent the default rendering and only show the customized version.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/d1c1d539-3d61-43dc-9561-785def6d2421.PNG" alt="Capture.PNG" class=" img-fluid img-markdown" /></p>
<pre><code>void AstraLabelStyle::draw(const QStyleOption* option, QPainter* p, const QWidget* widget) const
{  
    QWidget* nonConstWidget = const_cast&lt;QWidget*&gt;(widget);
    QLabel* label = dynamic_cast&lt;QLabel*&gt;(nonConstWidget);

    mTheme-&gt;getState(option);

    // Sub Elements
    QString text = label-&gt;text();

    // Font
    QFont font(mTheme-&gt;fontFamily(), 20, mTheme-&gt;fontWeight());
    
    // Compute the total size of subelements
    QSize eSize;
    int eSpacing;
    computeElementsSize(option, label, font, eSize, eSpacing);

    QRect rect = option-&gt;rect;
    
    // -------------------------------------------------------------------------------------------------------------
    int x = rect.x() + mTheme-&gt;paddingH(); ;
    int y = rect.y() + (rect.height() - eSize.height()) / 2;

    // Text
    if (!text.isEmpty())
    {
        p-&gt;save();
        p-&gt;setRenderHint(QPainter::TextAntialiasing, true);
        QRect textRect(x, y + (eSize.height() - option-&gt;fontMetrics.height()) / 2, option-&gt;fontMetrics.horizontalAdvance(text) * 1.5, option-&gt;fontMetrics.height());
        p-&gt;setPen(QPen(QBrush(mTheme-&gt;foregroundBrush(textRect)), 1));
        p-&gt;setFont(font);
        p-&gt;drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
        p-&gt;restore();
        
    }
}
</code></pre>
<p dir="auto">Thanks in advance for your help.</p>
]]></description><link>https://forum.qt.io/topic/158346/custom-qstyle-for-qlabel-issue-with-double-text-rendering</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 13:58:58 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/158346.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 22 Aug 2024 12:05:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Custom QStyle for QLabel: Issue with Double Text Rendering on Thu, 22 Aug 2024 18:07:31 GMT]]></title><description><![CDATA[<p dir="auto">At the end I have found the folowing solution:<br />
Now just one text rendering is performed.</p>
<pre><code>void AstraApplicationStyle::drawItemText(QPainter* painter, const QRect&amp; rect, int flags, const QPalette&amp; pal, bool enabled, const QString&amp; text, QPalette::ColorRole textRole) const
{
	if (text.isEmpty() || skipLabelTextRendering) 
	{
		skipLabelTextRendering = false;
		return; 
	}

	QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole);
}

void AstraApplicationStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
	//qDebug() &lt;&lt; "--&gt; Widget Name: " &lt;&lt; widget-&gt;objectName() &lt;&lt; " (CE: " &lt;&lt; element &lt;&lt; ")";

	QString name = widget-&gt;objectName();
	switch (element)
	{
		case QStyle::CE_ShapedFrame:
		{
			// QLabel
			if (widget &amp;&amp; qobject_cast&lt;const QLabel*&gt;(widget))
			{
				skipLabelTextRendering = true;
				mLabelStyle-&gt;draw(option, painter, widget);
				return;
			}
			break;
		}
		case QStyle::CE_PushButton:
		{
			mButtonStyle-&gt;draw(option, painter, widget);
			return;
		}
		default:
			break;
	}

	//Base
	QProxyStyle::drawControl(element, option, painter, widget);
}


</code></pre>
]]></description><link>https://forum.qt.io/post/807856</link><guid isPermaLink="true">https://forum.qt.io/post/807856</guid><dc:creator><![CDATA[Boltian]]></dc:creator><pubDate>Thu, 22 Aug 2024 18:07:31 GMT</pubDate></item><item><title><![CDATA[Reply to Custom QStyle for QLabel: Issue with Double Text Rendering on Thu, 22 Aug 2024 17:10:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a></p>
<p dir="auto">The draw() method is call up in the general style.<br />
mLabelStyle is a a helper which contains all necessry methods for define the style.</p>
<pre><code>void AstraApplicationStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
	QString name = widget-&gt;objectName();
	switch (element)
	{
		case QStyle::CE_ShapedFrame:
		{
			// QLabel
			if (widget &amp;&amp; qobject_cast&lt;const QLabel*&gt;(widget))
			{
				mLabelStyle-&gt;draw(option, painter, widget);
				return;
			}
			
			break;
		}
		case QStyle::CE_PushButton:
		{
			mButtonStyle-&gt;draw(option, painter, widget);
			return;
		}
		default:
			break;
	}

	//Base
	QProxyStyle::drawControl(element, option, painter, widget);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/807852</link><guid isPermaLink="true">https://forum.qt.io/post/807852</guid><dc:creator><![CDATA[Boltian]]></dc:creator><pubDate>Thu, 22 Aug 2024 17:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to Custom QStyle for QLabel: Issue with Double Text Rendering on Thu, 22 Aug 2024 13:12:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/boltian">@<bdi>Boltian</bdi></a> said in <a href="/post/807829">Custom QStyle for QLabel: Issue with Double Text Rendering</a>:</p>
<blockquote>
<pre><code>// Text
if (!text.isEmpty())
{
    p-&gt;save();
    p-&gt;setRenderHint(QPainter::TextAntialiasing, true);
    QRect textRect(x, y + (eSize.height() - option-&gt;fontMetrics.height()) / 2, option-&gt;fontMetrics.horizontalAdvance(text) * 1.5, option-&gt;fontMetrics.height());
    p-&gt;setPen(QPen(QBrush(mTheme-&gt;foregroundBrush(textRect)), 1));
    p-&gt;setFont(font);
    p-&gt;drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
    p-&gt;restore();
    
}
</code></pre>
</blockquote>
<p dir="auto">Hi,</p>
<p dir="auto">I'm not an expert when it comes to custom <code>QStyle</code> implementations, but I wonder what happens, when you remove the <code>save</code> / <code>restore</code> calls?</p>
<p dir="auto">Edit:</p>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said above:<br />
Check the source for <code>QLabel</code> style:</p>
<p dir="auto">In <code>paintEvent</code> the <code> style-&gt;drawItemText</code> is called</p>
<ul>
<li><a href="https://codebrowser.dev/qt6/qtbase/src/widgets/widgets/qlabel.cpp.html#1063" target="_blank" rel="noopener noreferrer nofollow ugc">https://codebrowser.dev/qt6/qtbase/src/widgets/widgets/qlabel.cpp.html#1063</a></li>
</ul>
<p dir="auto">and internally <code>drawItemText</code> paints with <code>painter-&gt;drawText</code></p>
<ul>
<li><a href="https://codebrowser.dev/qt6/qtbase/src/widgets/styles/qstyle.cpp.html#593" target="_blank" rel="noopener noreferrer nofollow ugc">https://codebrowser.dev/qt6/qtbase/src/widgets/styles/qstyle.cpp.html#593</a></li>
</ul>
]]></description><link>https://forum.qt.io/post/807833</link><guid isPermaLink="true">https://forum.qt.io/post/807833</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Thu, 22 Aug 2024 13:12:04 GMT</pubDate></item><item><title><![CDATA[Reply to Custom QStyle for QLabel: Issue with Double Text Rendering on Thu, 22 Aug 2024 12:21:59 GMT]]></title><description><![CDATA[<p dir="auto">Who calls draw()? It's not a virtual function of QStyle.</p>
]]></description><link>https://forum.qt.io/post/807832</link><guid isPermaLink="true">https://forum.qt.io/post/807832</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Thu, 22 Aug 2024 12:21:59 GMT</pubDate></item></channel></rss>