<?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[[SOLVED] Creating classes in Qt Stylesheet]]></title><description><![CDATA[<p dir="auto">Hi all!</p>
<p dir="auto">I’m new to Qt and I have a question regarding stylesheets. I’m using many labels in my designed and I want each label to have a different styling. As of now, I have</p>
<p dir="auto">@QLabel<br />
{<br />
border: none;<br />
padding: 0;<br />
background: none;<br />
}@</p>
<p dir="auto">but this applies the same style to all the labels. I even tried<br />
@<br />
.heading<br />
{<br />
color: brown;<br />
}</p>
<p dir="auto">.heading2<br />
{<br />
color: red;<br />
}@</p>
<p dir="auto">for different styles for each label. But I can’t get it to work. My question is, how can I customize the styling for each label even though I am referring to the same stylesheet?<br />
Thanks.</p>
]]></description><link>https://forum.qt.io/topic/23357/solved-creating-classes-in-qt-stylesheet</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 18:56:20 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/23357.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 18 Jan 2013 08:49:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Wed, 07 Aug 2013 16:26:35 GMT]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">Is there a way to have the same result as <a class="plugin-mentions-user plugin-mentions-a" href="/user/jazzycamel">@<bdi>jazzycamel</bdi></a> example on the hLabel but without registering the object using the setObjectName() method?</p>
<p dir="auto">In other words, specify within the stylesheet directly that the style must be applied to hLabel only which is a member of the class Widget.</p>
<p dir="auto">Why is the setObjectName() necessary?</p>
<p dir="auto">The reason I am asking this is, let's say you have a series of custom dialogs (e.g. dialog1-dialog3) with each one having as a member a QPushButton with the same name "nextButton". Then using the setObjectName() approach one needs to register each "nextButton" with a different identifier if he wants to apply a separate style to each one.</p>
<p dir="auto">What I am looking for is a way to identify directly in the stylesheet each nextButton from the dialog it is a member of.</p>
<p dir="auto">Thanks for your help.</p>
]]></description><link>https://forum.qt.io/post/175528</link><guid isPermaLink="true">https://forum.qt.io/post/175528</guid><dc:creator><![CDATA[ochampao]]></dc:creator><pubDate>Wed, 07 Aug 2013 16:26:35 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 10:52:45 GMT]]></title><description><![CDATA[<p dir="auto">sierdzio</p>
<p dir="auto">You were right! Running</p>
<p dir="auto">@<br />
ui-&gt;label-&gt;setStyleSheet(”“);<br />
@</p>
<p dir="auto">worked like a charm! Thank you so much for your time :)</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jazzycamel">@<bdi>jazzycamel</bdi></a>: Thanks to you too for providing a nice example.</p>
]]></description><link>https://forum.qt.io/post/163611</link><guid isPermaLink="true">https://forum.qt.io/post/163611</guid><dc:creator><![CDATA[holygirl]]></dc:creator><pubDate>Fri, 18 Jan 2013 10:52:45 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 10:32:55 GMT]]></title><description><![CDATA[<p dir="auto">The following is a full working example with C++, QSS and QRC sources:</p>
<p dir="auto"><em>main.cpp</em><br />
@<br />
#include &lt;QtGui&gt;</p>
<p dir="auto">class Widget : public QWidget<br />
{</p>
<p dir="auto">public:<br />
Widget(QWidget *parent = 0){<br />
Q_UNUSED(parent)</p>
<pre><code>    QVBoxLayout *l=new QVBoxLayout(this);
    QLabel *hLabel=new QLabel("Heading Label", this);
    hLabel-&gt;setObjectName("headingLabel");
    l-&gt;addWidget(hLabel);

    l-&gt;addWidget(new QLabel("Other Label", this));
}
</code></pre>
<p dir="auto">};</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QApplication a(argc, argv);</p>
<pre><code>QFile sFile&amp;#40;":/main.qss"&amp;#41;;
if(sFile.open(QFile::ReadOnly)){
    QString style(sFile.readAll());
    a.setStyleSheet(style);
}

Widget w;
w.show();
return a.exec&amp;#40;&amp;#41;;
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto"><em>main.qss</em><br />
@<br />
QLabel {<br />
background-color: pink;<br />
}</p>
<p dir="auto">QLabel#headingLabel {<br />
color: brown;<br />
}<br />
@</p>
<p dir="auto"><em>example.qrc</em><br />
@<br />
&lt;RCC&gt;<br />
&lt;qresource&gt;<br />
&lt;file&gt;main.qss&lt;/file&gt;<br />
&lt;/qresource&gt;<br />
&lt;/RCC&gt;<br />
@</p>
<p dir="auto">Hope this helps ;o)</p>
]]></description><link>https://forum.qt.io/post/163608</link><guid isPermaLink="true">https://forum.qt.io/post/163608</guid><dc:creator><![CDATA[jazzycamel]]></dc:creator><pubDate>Fri, 18 Jan 2013 10:32:55 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 10:16:59 GMT]]></title><description><![CDATA[<p dir="auto">Wow, and it works in general? Qt uses unix paths. No need to use double forward slashes like that.</p>
<p dir="auto">I don't know the answer. This code should be enough. Most probably the object name is changed <em>after</em> stylesheet is loaded, so it is not updated. Try either loading QSS later, or run ui-&gt;label-&gt;setStyleSheet(""); after you set the object name. Maybe that would trigger a redraw.</p>
]]></description><link>https://forum.qt.io/post/163606</link><guid isPermaLink="true">https://forum.qt.io/post/163606</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 18 Jan 2013 10:16:59 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 10:00:48 GMT]]></title><description><![CDATA[<p dir="auto">That's odd. My label doesn't seem to be brown in color! I've also included the stylesheet in my main.cpp as</p>
<p dir="auto">@<br />
QFile styleFile( "://new//prefix1//the-path.qss" );<br />
styleFile.open( QFile::ReadOnly );<br />
QString style( styleFile.readAll() );<br />
app.setStyleSheet( style );<br />
@</p>
<p dir="auto">Is there anything else I need to include? Could you please a provide an example? You've been a lot of help to me. Thanks.</p>
]]></description><link>https://forum.qt.io/post/163605</link><guid isPermaLink="true">https://forum.qt.io/post/163605</guid><dc:creator><![CDATA[holygirl]]></dc:creator><pubDate>Fri, 18 Jan 2013 10:00:48 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 09:47:54 GMT]]></title><description><![CDATA[<p dir="auto">It is correct, yes.</p>
]]></description><link>https://forum.qt.io/post/163602</link><guid isPermaLink="true">https://forum.qt.io/post/163602</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 18 Jan 2013 09:47:54 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 09:44:27 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the reply sierdzio. I went through the document and tried implementing the id selector but I think I'm doing something wrong. In my .qss, I have</p>
<p dir="auto">@<br />
QLabel#heading<br />
{<br />
color: brown;<br />
}<br />
@</p>
<p dir="auto">and in my code, I call it as</p>
<p dir="auto">@<br />
ui-&gt;label-&gt;setObjectName("heading");<br />
@</p>
<p dir="auto">Is this syntax right? Could you please guide me in the right direction?</p>
]]></description><link>https://forum.qt.io/post/163601</link><guid isPermaLink="true">https://forum.qt.io/post/163601</guid><dc:creator><![CDATA[holygirl]]></dc:creator><pubDate>Fri, 18 Jan 2013 09:44:27 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Creating classes in Qt Stylesheet on Fri, 18 Jan 2013 08:57:30 GMT]]></title><description><![CDATA[<p dir="auto">You need to use id selector:<br />
@<br />
QLabel#myObjectName {}<br />
@</p>
<p dir="auto">myObjectName is the objectName property of QObject (QObject::setObjectName()). More about QSS here: "link":<a href="http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html</a>.</p>
<p dir="auto">Just be sure not to overload the thing. QSS parsing takes time and can noticeably impact the performance if it grows too big.</p>
]]></description><link>https://forum.qt.io/post/163590</link><guid isPermaLink="true">https://forum.qt.io/post/163590</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 18 Jan 2013 08:57:30 GMT</pubDate></item></channel></rss>