<?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[Set stylesheet to parent frame only]]></title><description><![CDATA[<p dir="auto">Hi everybody,</p>
<p dir="auto">I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color. But I can't for the life of me figure it out. Here are some observations:</p>
<ol>
<li>
<p dir="auto">Trying to set the stylesheet to only objects of a certain object name via # doesn't work if such object doesn't exist (as expected).</p>
</li>
<li>
<p dir="auto">But: trying to set the stylesheet to only objects of a certain object name via # doesn't prevent subwidgets from inheriting the stylesheet. Even if they have an object name of their own.</p>
</li>
<li>
<p dir="auto">This is true even if the class isn't really the same. I tried making a subclass for the main frame ("AFrame"), that inherits from QFrame, but left the subframe as QFrame proper. The QFrame and QLabel still took on the AFrame's stylesheet.</p>
</li>
<li>
<p dir="auto">This isn't true in the other direction, though. The QFrame subwidget accepts the parent's AFrame stylesheet, but a stylesheet set directly with AFrame doesn't work (as expected).</p>
</li>
<li>
<p dir="auto">I can't find a way to delete the stylesheet of the subwidgets. I can set a different color, but I can't put them back to the default color.</p>
<pre><code>
AFrame *vMainFrame = new AFrame(this);
vMainFrame-&gt;setObjectName("AFrame");
QHBoxLayout *vLayoutMainFrame = new QHBoxLayout(vMainFrame);
ui-&gt;verticalLayout_2-&gt;addWidget(vMainFrame);

QFrame *vSubFrame = new QFrame(this);
vSubFrame-&gt;setObjectName("SOMEOTHERFRAME");
vLayoutMainFrame-&gt;addWidget(vSubFrame);

QHBoxLayout *vLayoutSubFrame = new QHBoxLayout(vSubFrame);
QLabel* vLabel = new QLabel("womimwolmiplomw",this);
vLabel-&gt;setObjectName("somelabel");
vLayoutSubFrame-&gt;addWidget(vLabel);


//main no color, sub frame no color, label is green
//vMainFrame-&gt;setStyleSheet(QString("QFrame#somelabel {background-color: green}"));

//main no color, sub frame + label green
vMainFrame-&gt;setStyleSheet(QString("QFrame#SOMEOTHERFRAME {background-color: green}"));

//no color
//vMainFrame-&gt;setStyleSheet(QString("AFrame#SOMEOTHERFRAME {background-color: green}"));

//main is blue, sub frame + label green
//vMainFrame-&gt;setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));

//main is blue, sub frame + label blue
//vMainFrame-&gt;setStyleSheet(QString("AFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
//vMainFrame-&gt;setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: }"));
//vMainFrame-&gt;setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));

//no color
//vMainFrame-&gt;setStyleSheet(QString("AFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
//vMainFrame-&gt;setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame {}"));

//no color for main, sub frame + label green
//vMainFrame-&gt;setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));

</code></pre>
</li>
</ol>
<pre><code>    

Best wishes,
Angela</code></pre>
]]></description><link>https://forum.qt.io/topic/164256/set-stylesheet-to-parent-frame-only</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 01:25:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164256.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Feb 2026 10:31:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Mon, 16 Feb 2026 07:53:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/836241">Set stylesheet to parent frame only</a>:</p>
<blockquote>
<p dir="auto">If you want to change the background of only two widget, going the palette way would be easier</p>
</blockquote>
<p dir="auto">Changing the palette works unreliably on some OSes. I believe it only works properly with the Fusion style. But, if you want to go with a native look on Windows (and maybe macOS as well) you need to use stylesheets (which also have their problems). For portable code I can only advise againt using the palette.</p>
]]></description><link>https://forum.qt.io/post/836319</link><guid isPermaLink="true">https://forum.qt.io/post/836319</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 16 Feb 2026 07:53:16 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 19:01:43 GMT]]></title><description><![CDATA[<p dir="auto">Another observation: if I set the palette(window) background directly on the line edit, it becomes white, but the frame and label are blue (as expected). But if I set the palette(window) to both the subframe and the line edit, the line edit is still grey. It's all a bit odd.</p>
]]></description><link>https://forum.qt.io/post/836294</link><guid isPermaLink="true">https://forum.qt.io/post/836294</guid><dc:creator><![CDATA[angela2016]]></dc:creator><pubDate>Sat, 14 Feb 2026 19:01:43 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 18:46:45 GMT]]></title><description><![CDATA[<p dir="auto">Not sure if you can see it well but here's a screenshot.</p>
<p dir="auto">The top example is my QPalette code. Second is the default without any setting of the background. The third and fourth window are with your code: palette(window) and palette(base) produce the same result. The label is as expected, but the line edit has the same color as the label, not as the default line edit.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/3a9a6036-790a-4e94-9355-2dfc6e0cda63.png" alt="a50db0b1-7577-45cf-833f-06b884499f15-image.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/836293</link><guid isPermaLink="true">https://forum.qt.io/post/836293</guid><dc:creator><![CDATA[angela2016]]></dc:creator><pubDate>Sat, 14 Feb 2026 18:46:45 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 15:17:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/angela2016">@<bdi>angela2016</bdi></a><br />
Oh, so after the <code>palette(window)</code> you are not back where you started, something has still changed?  And same for <code>palette(base)</code>?</p>
]]></description><link>https://forum.qt.io/post/836278</link><guid isPermaLink="true">https://forum.qt.io/post/836278</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 14 Feb 2026 15:17:23 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 15:02:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thanks, that's almost it. Line edits and check boxes aren't white enough, though, compared to the palette solution. It's all "label grey".</p>
]]></description><link>https://forum.qt.io/post/836276</link><guid isPermaLink="true">https://forum.qt.io/post/836276</guid><dc:creator><![CDATA[angela2016]]></dc:creator><pubDate>Sat, 14 Feb 2026 15:02:47 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 10:15:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/angela2016">@<bdi>angela2016</bdi></a> said in <a href="/post/836215">Set stylesheet to parent frame only</a>:</p>
<blockquote>
<p dir="auto">I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color</p>
</blockquote>
<p dir="auto">Sorry I'm a bit late to the party.  You want the top level frame to have a background color but its child frame plus its content like button/label not to inherit that, right?  Then <code>background-color: palette(base)</code> or <code>background-color: palette(window)</code> on the child widget to cancel what is inherited from the parent widget seems to do what you want?  And the latter is presumably the equivalent of what you have written in your code:</p>
<pre><code>#include &lt;QFrame&gt;
#include &lt;QPushButton&gt;

#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFrame f1;
    f1.setGeometry(50, 50, 200, 200);
    f1.setStyleSheet("background-color: red;");
    QFrame f2(&amp;f1);
    f2.setGeometry(25, 25, 100, 100);
    f2.setStyleSheet("background-color: palette(window)");  // or palette(base)
    QPushButton pb(&amp;f2);
    pb.setText("Hello");
    pb.setGeometry(20, 20, 50, 50);
    f1.show();
    return a.exec();
}
</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/2a591094-447f-4cb8-9771-9510df80f6e2.png" alt="Screenshot 2026-02-14 095121.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Does this give you what you ask for?</p>
]]></description><link>https://forum.qt.io/post/836255</link><guid isPermaLink="true">https://forum.qt.io/post/836255</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 14 Feb 2026 10:15:57 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Sat, 14 Feb 2026 03:36:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Thanks for the hint. I eventually got it working with:</p>
<pre><code>    QPalette palette = vMainFrame-&gt;palette();
    palette.setColor(QPalette::Window, QColor("blue"));
    vMainFrame-&gt;setAutoFillBackground( true );
    vMainFrame-&gt;setPalette(palette);

    QPalette p;
    QColor c = p.color(QPalette::Window);
    p.setColor(QPalette::Window, c);
    vSubFrame-&gt;setAutoFillBackground( true );
    vSubFrame-&gt;setPalette(p);
</code></pre>
<p dir="auto">It should be said that I found the second part on the old Qt forum and another user was apparently horrified by it, but it's the only way of resetting the palette I found that actually worked.</p>
]]></description><link>https://forum.qt.io/post/836252</link><guid isPermaLink="true">https://forum.qt.io/post/836252</guid><dc:creator><![CDATA[angela2016]]></dc:creator><pubDate>Sat, 14 Feb 2026 03:36:50 GMT</pubDate></item><item><title><![CDATA[Reply to Set stylesheet to parent frame only on Fri, 13 Feb 2026 19:43:25 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">If you want to change the background of only two widget, going the palette way would be easier:</p>
<pre><code>QPalette *palette = vMainFrame-&gt;palette();
palette.setColor(QPalette.Window, QColor("green"));
vMainFrame-&gt;setPalette(palette);
</code></pre>
<p dir="auto">And the same for your other widget.</p>
]]></description><link>https://forum.qt.io/post/836241</link><guid isPermaLink="true">https://forum.qt.io/post/836241</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 13 Feb 2026 19:43:25 GMT</pubDate></item></channel></rss>