<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hello Matic, <div><br></div><div>I was looking at your issue and was trying to think how to help. But I was confused by what you said:</div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">and the tabbar's view snaps to the right. This has been very annoying for users as when they switch<br>to an inactive QTabWidget that becomes active, the whole tabbar view changes.</blockquote></div><div>Do you mean that the tabs actually snap to the right, like move? Would you care to explain just a bit more?</div><div><br></div><div>As for inactive and active tabs, </div><div><br></div><div><div><font face="monospace">style_sheet = """QTabBar::tab {background: grey; }</font></div><div><span style="font-family:monospace;color:rgb(0,0,0)"> QTabBar::tab:selected { color: red; }</span></div><div style="color:rgb(0,0,0)"><font face="monospace"> QTabBar::tab:!selected {color: blue; }"""</font></div><div><font face="monospace">self.tabWidget.setStyleSheet(style_sheet</font><span style="font-family:monospace">)</span></div></div><div><br></div><div>Not sure if that helps your case, I know you said stylesheets aren't helping. But it is possible to change the color and appearance of a tab depending upon whether or not it is selected with style sheets. Was trying to understand more how your QTabWidget is set up to better help. Perhaps manipulating the style sheets more may help give the tabs an appearance of enabled or disabled. Not sure if you have looked into that option. </div><div><br></div><div>You can also play around with the QTabWidget::pane and QTabWidget::tab-bar using style sheets, perhaps changing their alignment and position depending upon your circumstances. </div><div><br></div><div>I have always found it easier to manipulate the appearance of widgets with style sheets, rather than using QPalette. </div><div><br></div><div>Joshua Willman</div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jun 4, 2021 at 10:04 PM Matic Kukovec <<a href="mailto:kukovecmatic@hotmail.com">kukovecmatic@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hello Maurizio,</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
You are right about the superfluous stuff, I copy pasted from an actual working example.<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
The painter save/restore is still in there because I forgot to remove it in this example.<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
And yes, <b><i>data </i></b>is the PyQt5 package with all sub-packages, all of them in one namespace.</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
The reason stylesheets are not usable in my case, is that I need to switch between an</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
active and inactive mode, and when setting a new stylesheet, the scroll offset is not remembered</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
and the tabbar's view snaps to the right. This has been very annoying for users as when they switch</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
to an inactive QTabWidget that becomes active, the whole tabbar view changes. When you have</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
a lot of tabs, then the user sometimes doesn't know where the active tab "jumped" to.<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
That is why I'm using the overridden <i><b>paintEvent</b></i>.</div>
<blockquote style="border-color:rgb(200,200,200);border-left-width:3px;border-left-style:solid;padding-left:1ex;margin-left:0.8ex;color:rgb(102,102,102)">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Anyway, the main issue here is that you're probably using Windows, and the Windows QStyle does paint some things a bit differently, including possibly ignoring the palette of the style option (but, hey, that could also be a bug, unfortunately I cannot test
it).</div>
</blockquote>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Yes, I'm on Windows.</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Thanks for the help, I will investigate further.</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
If anyone can shed more light on where the problem could be, please let me know.<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Regards,</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Matic<br>
</div>
<div id="gmail-m_-6564885657798896321appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-6564885657798896321divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Maurizio Berti <<a href="mailto:maurizio.berti@gmail.com" target="_blank">maurizio.berti@gmail.com</a>><br>
<b>Sent:</b> Friday, June 4, 2021 3:46 PM<br>
<b>To:</b> Matic Kukovec <<a href="mailto:kukovecmatic@hotmail.com" target="_blank">kukovecmatic@hotmail.com</a>><br>
<b>Cc:</b> <a href="mailto:pyqt@riverbankcomputing.com" target="_blank">pyqt@riverbankcomputing.com</a> <<a href="mailto:pyqt@riverbankcomputing.com" target="_blank">pyqt@riverbankcomputing.com</a>><br>
<b>Subject:</b> Re: QTabBar tab text painting?</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">
<div>Hello Matic,</div>
<div><br>
</div>
The whole resetting the palette in the for loop is completely unnecessary (especially if done every time in a for loop, you can create a palette the first time, and then overwrite opt.palette, but that would be unnecessary anyway since the palette should not
change between tabs).
<div>Also, the painter save/restore is completely pointless like that: the point of saving the state of the painter is that you can temporarily change painter settings and restore the previous state (which should also match the state level: it's not really
clear since the formatting is not monospaced, but it seems that you're calling painter.restore() in the for loop, and if that's the case, you should not, as you'd have unmatched levels of saved states). Since you're not painting anything before or after that,
there's no benefit.</div>
<div><br>
</div>
<div>Anyway, the main issue here is that you're probably using Windows, and the Windows QStyle does paint some things a bit differently, including possibly ignoring the palette of the style option (but, hey, that could also be a bug, unfortunately I cannot
test it).</div>
<div><br>
</div>
<div>That said, if you only want to change the color of the tab text, subclassing and overriding the paintEvent is a bit too much (and also risky: your implementation doesn't take into account movable tabs, which is really tricky). I'd suggest a more simpler:</div>
<div><br>
</div>
<div><font face="monospace">self.tabWidget.setStyleSheet('QTabBar { color: red; }')</font></div>
<div><br>
</div>
<div>Which should solve the whole problem on any platform.</div>
<div><br>
</div>
<div>Be aware that it's very important that you use the class selector (unless you set the stylesheet on the tab bar), if you use generic/universal properties you'll get unexpected results, especially for a container widget such a QTabWidget.</div>
<div>So, *never* do the following: it works, but it works very badly.</div>
<div><br>
</div>
<div><font face="monospace">self.tabWidget.setStyleSheet('color: red') # NO!</font></div>
<div><br>
</div>
<div><br>
</div>
<div>Maurizio</div>
<div><br>
</div>
<div>PS: it took me a while to understand what "data" actually was; I'd strongly suggest you against that choice, "data" is a very generic word and doesn't reflect what it refers to in your code (and it's also very confusing to others reading your code): what
happens whenever you have a function that *actually* uses some "data"? Why should you then choose another word instead of an obvious name, because you already used that name for a wrong reference and you cannot overwrite it because it's required in the function
scope?</div>
</div>
</div>
<br>
<div>
<div dir="ltr">Il giorno ven 4 giu 2021 alle ore 10:26 Matic Kukovec <<a href="mailto:kukovecmatic@hotmail.com" target="_blank">kukovecmatic@hotmail.com</a>> ha scritto:<br>
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hi guys,</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
I have a subclassed QTabBar which I want to manually change the color of each tab.</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
After a lot of trial-and-error, I tried this to make all the tabs red:</div>
<blockquote style="margin-top:0px;margin-bottom:0px">
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt">def paintEvent(self, event):</span></i>
<div><i><span style="font-size:10pt"> painter = data.QStylePainter(self)</span></i></div>
<div><i><span style="font-size:10pt"> opt = data.QStyleOptionTab()</span></i></div>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt"> painter.save()</span></i></div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt"> </span><br>
</i></div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt"> for i in range(self.count()):</span></i>
<div><i><span style="font-size:10pt"> self.initStyleOption(opt, i)</span></i></div>
<div><i><span style="font-size:10pt"> items = (</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Window,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Background,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.WindowText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Foreground,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Base,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.AlternateBase,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.ToolTipBase,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.ToolTipText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.PlaceholderText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Text,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Button,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.ButtonText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.BrightText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Light,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Midlight,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Dark,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Mid,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Shadow,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Highlight,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.HighlightedText,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Link,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.LinkVisited,</span></i></div>
<div><i><span style="font-size:10pt"> )</span></i></div>
<div><i><span style="font-size:10pt"> groups = (</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Disabled,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Active,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Inactive,</span></i></div>
<div><i><span style="font-size:10pt"> data.QPalette.Normal,</span></i></div>
<div><i><span style="font-size:10pt"> )</span></i></div>
<div><i><span style="font-size:10pt"> color = data.QColor(0xffff0000)</span></i></div>
<div><i><span style="font-size:10pt"> for g in groups:</span></i></div>
<div><i><span style="font-size:10pt"> for p in items:</span></i></div>
<div><i><span style="font-size:10pt"> opt.palette.setColor(g, p, color)</span></i></div>
<div><i><span style="font-size:10pt"> opt.palette.setBrush(g, p, color)</span></i></div>
<div><i><span style="font-size:10pt"> painter.setBrush(data.QBrush(data.QColor(color)))</span></i></div>
<div><i><span style="font-size:10pt"> painter.setPen(data.QPen(data.QColor(color)))</span></i></div>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt"> painter.drawControl(data.QStyle.CE_TabBarTabLabel, opt)</span></i></div>
</blockquote>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><br>
</i></div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<i><span style="font-size:10pt"> painter.restore()</span></i><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Now this works, but only for the non-currently selected tabs, the selected tab ALWAYS has black text and I have no idea why.</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Here is a screenshot: <img size="4559" style="max-width: 100%;" src="cid:179d78372e9cb971f161"><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
The "string.h" tab is the currently selected one.<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Any ideas what I'm doing wrong or missing?</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Thanks,</div>
<div style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Matic<br>
</div>
</div>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br>
<a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>
</div>
</div>
</blockquote></div>