<?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[QStyleOptionButton cloud be set background color?]]></title><description><![CDATA[<p dir="auto">QStyleOptionButton cloud be set background color?</p>
<p dir="auto">There's a table that's show some information of a group device, I use model+qtableview,one column of the table is show switch for user, and user need the power of control the <a href="http://switch.so" target="_blank" rel="noopener noreferrer nofollow ugc">switch.so</a> i use QStyledItemDelegate to show a button in this column.For better user experience,i hope the switch(QStyleOptionButton)is yellow when the device is running and green when the deivce is stop.<br />
<img src="https://img-bbs.csdn.net/upload/201807/03/1530601898_812730.png" alt="alt text" class=" img-fluid img-markdown" /></p>
<pre><code>void ChooseWatchObjectDelegate::paint(QPainter* painter, const QStyleOptionViewItem&amp; option, const QModelIndex&amp; index) const
{
    QStyleOptionButton btn;
    btn.rect = option.rect;
    bool fg = index.model()-&gt;data(index).toBool();
    if(fg == true)
    {
        btn.palette.setBrush(QPalette::Button,QBrush(QColor(Qt::green)));//it doesn't work.....
        btn.text = tr("启动");
    }
    else
    {
        btn.palette.setBrush(QPalette::Background,QBrush(QColor(Qt::red)));//it also doesn't work.....
        btn.text = tr("停止");
    }
    btn.state |= QStyle::State_Enabled;
    painter-&gt;setBrush(QColor(Qt::red));  //it's still unuseful...
    if(option.widget != NULL)
        QApplication::style()-&gt;drawControl(QStyle::CE_PushButton,&amp;btn,painter);
    else
        option.widget-&gt;style();
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/92249/qstyleoptionbutton-cloud-be-set-background-color</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 07:52:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/92249.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 04 Jul 2018 01:44:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Mon, 16 Jul 2018 05:25:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/johnmpaine">@<bdi>JohnMPaine</bdi></a><br />
tried....<br />
And then ,the whole button has filled....the txt of button can't be seen.....</p>
]]></description><link>https://forum.qt.io/post/469692</link><guid isPermaLink="true">https://forum.qt.io/post/469692</guid><dc:creator><![CDATA[qazaq408]]></dc:creator><pubDate>Mon, 16 Jul 2018 05:25:36 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Fri, 13 Jul 2018 07:29:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qazaq408">@<bdi>qazaq408</bdi></a> said in <a href="/post/468226">QStyleOptionButton cloud be set background color?</a>:</p>
<blockquote>
<p dir="auto">btn.palette.setBrush(QPalette::Background,QBrush(QColor(Qt::red)));<br />
painter-&gt;fillRect(btn.rect,btn.palette.brush(QPalette::Button));  //unused.......</p>
</blockquote>
<p dir="auto">Have you tried<br />
painter-&gt;fillRect(btn.rect, btn.palette.brush(QPalette::Background));<br />
instead of<br />
painter-&gt;fillRect(btn.rect, btn.palette.brush(QPalette::Button));</p>
]]></description><link>https://forum.qt.io/post/469274</link><guid isPermaLink="true">https://forum.qt.io/post/469274</guid><dc:creator><![CDATA[JohnMPaine]]></dc:creator><pubDate>Fri, 13 Jul 2018 07:29:13 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Wed, 11 Jul 2018 10:15:35 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
I think i used QStyle::CE_PushButtonLabel instead to get it to be transparent as<br />
QStyle::CE_PushButton ignored all QBrush.<br />
If that fails too, you could use icon on button to show state.</p>
]]></description><link>https://forum.qt.io/post/468833</link><guid isPermaLink="true">https://forum.qt.io/post/468833</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 11 Jul 2018 10:15:35 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Wed, 11 Jul 2018 09:11:23 GMT]]></title><description><![CDATA[<p dir="auto">Could someone give me help......<br />
on those days,I trid to use a pix to replace the button,but when I clicked the button(It's is a pix in fact),it doesn't look like a button,because the button will changed when it clicked by mouse but pix not<br />
More important,my boss denied my plan that's replace the button by a pix....<br />
<img src="https://img-blog.csdn.net/20180711171048964?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FhemFxNDA4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/468812</link><guid isPermaLink="true">https://forum.qt.io/post/468812</guid><dc:creator><![CDATA[qazaq408]]></dc:creator><pubDate>Wed, 11 Jul 2018 09:11:23 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Mon, 09 Jul 2018 01:31:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a><br />
win7 + Qt5.9 + mingw32</p>
<p dir="auto">I had tried</p>
<pre><code>    if(option.widget != NULL)
    {
        painter-&gt;fillRect(btn.rect,btn.palette.brush(QPalette::Button)); //It doesn't work
        QApplication::style()-&gt;drawControl(QStyle::CE_PushButton,&amp;btn,painter);
    }
    else
        option.widget-&gt;style();
</code></pre>
<p dir="auto">I also tried</p>
<pre><code>    if(option.widget != NULL)
    {
        btn.palette.setBrush(QPalette::Background,QBrush(QColor(Qt::red)));
        painter-&gt;fillRect(btn.rect,btn.palette.brush(QPalette::Button));  //unused.......
        QApplication::style()-&gt;drawControl(QStyle::CE_PushButton,&amp;btn,painter);
    }
    else
        option.widget-&gt;style();
</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/386a1b2b-4c9d-46f2-8316-3e2ed090c731.png" alt="0_1531099806126_23333.png" class=" img-fluid img-markdown" /><br />
It's a question  ~。~</p>
]]></description><link>https://forum.qt.io/post/468226</link><guid isPermaLink="true">https://forum.qt.io/post/468226</guid><dc:creator><![CDATA[qazaq408]]></dc:creator><pubDate>Mon, 09 Jul 2018 01:31:22 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Fri, 06 Jul 2018 11:01:33 GMT]]></title><description><![CDATA[<p dir="auto">You might want to call</p>
<p dir="auto"><code>painter-&gt;fillRect(btn.rect, btn.palette.brush(QPalette::Button));</code></p>
<p dir="auto">Before drawing the button on top of it.</p>
]]></description><link>https://forum.qt.io/post/467823</link><guid isPermaLink="true">https://forum.qt.io/post/467823</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 06 Jul 2018 11:01:33 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Fri, 06 Jul 2018 10:07:55 GMT]]></title><description><![CDATA[<p dir="auto">Sorry, my bad, I misread your code.</p>
<p dir="auto">What platform are you on ?<br />
With which version of Qt ?</p>
<p dir="auto">Can you also show what you are currently getting ?</p>
]]></description><link>https://forum.qt.io/post/467799</link><guid isPermaLink="true">https://forum.qt.io/post/467799</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 06 Jul 2018 10:07:55 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Fri, 06 Jul 2018 10:03:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a><br />
Thank you..</p>
<p dir="auto">you mean that's create a new painter?</p>
<pre><code> painter-&gt;setBrush(QColor(Qt::red));  //it's still unuseful...
</code></pre>
<p dir="auto">It doesn't work...</p>
]]></description><link>https://forum.qt.io/post/467797</link><guid isPermaLink="true">https://forum.qt.io/post/467797</guid><dc:creator><![CDATA[qazaq408]]></dc:creator><pubDate>Fri, 06 Jul 2018 10:03:25 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Fri, 06 Jul 2018 10:09:09 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto"><s>You are modifying the <code>painter</code> object but you are then calling the style of QApplication to paint. Why don't use use <code>painter</code> ?</s></p>
<p dir="auto">Misread the code.</p>
<p dir="auto"><em>[edit: SGaist]</em></p>
]]></description><link>https://forum.qt.io/post/467445</link><guid isPermaLink="true">https://forum.qt.io/post/467445</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 06 Jul 2018 10:09:09 GMT</pubDate></item><item><title><![CDATA[Reply to QStyleOptionButton cloud be set background color? on Wed, 04 Jul 2018 10:05:38 GMT]]></title><description><![CDATA[<p dir="auto">I try to use setStyleSheet in the delegate</p>
<pre><code>setStyleSheet("QPushButton{background-color:red}");
</code></pre>
<p dir="auto">it still doesn't work....</p>
]]></description><link>https://forum.qt.io/post/467337</link><guid isPermaLink="true">https://forum.qt.io/post/467337</guid><dc:creator><![CDATA[qazaq408]]></dc:creator><pubDate>Wed, 04 Jul 2018 10:05:38 GMT</pubDate></item></channel></rss>