[Qt-interest] Setting the background color of a tab

Jonas Gehring jonas.gehring at boolsoft.org
Thu Aug 4 12:51:24 CEST 2011


You're accessing the current index of the tab bar, not the index of the
tab you're about to paint. I think Jake intended something like this:

> http://jgehring.net/files/6987108e463f05d849/color_tabs.png

I don't see how you could implement this functionality (tab at 0 is
green, tab at i is RRGGBB) with a custom style. For three tabs, flags
like beginning or middle are sufficient, but you for a larger number of
tabs this won't work.

- Jonas

On 08/04/2011 12:43 PM, Frank Hemer wrote:
> On Thursday 04 August 2011 12:35:45 Jonas Gehring wrote:
>> Implementing a custom QStyle would impose the same limitations as using
>> style sheets, wouldn't it? You're still unable to set a specific color
>> for the tab at index i.
> 
> I use it that way and it works like a charm ...
> 
> Note that you have some access to the idx - see here:
> 
>       if (el == QStyle::CE_TabBarTab) {
>          const QTabBar * tabBar = qobject_cast <const QTabBar *> (w);
>          int tabPos = tabBar->currentIndex ();
>          const QStyleOptionTab * t = static_cast <const QStyleOptionTab *> 
> (opt);
>          if (t) {
>             p->save ();
>             QStyleOptionTab tab (*t);
>             if (t->state.testFlag (QStyle::State_Selected)) {
>                if (tab.position == QStyleOptionTab::Beginning) {
>                   tab.palette.setBrush (QPalette::Window, Qt::green);
>                } else if (tab.position == QStyleOptionTab::Middle) {
>                   if (tabPos == 1) {
>                      tab.palette.setBrush (QPalette::Window, Qt::yellow);
>                   } else if (tabPos == 2) {
>                      tab.palette.setBrush (QPalette::Window, Qt::cyan);
>                   }
>                } else {
>                   tab.palette.setBrush (QPalette::Window, Qt::red);
>                }
> 
> 
> Frank
> 
>>
>> On 08/04/2011 11:52 AM, Frank Hemer wrote:
>>> On Thursday 04 August 2011 11:45:27 Jonas Gehring wrote:
>>>> On 08/04/2011 11:41 AM, Stefan Löffler wrote:
>>>>> Wouldn't it be better to simply override initStyleOption in your
>>>>> subclass? Something like (not tested)
>>>>
>>>> Unfortunately, initStyleOption() is not virtual, so you can't override
>>>> it. I don't have a clue why this function is non-virtual for QTabBar and
>>>> many other widgets, since this would be an easy way to extend their
>>>> visual representation.
>>>>
>>>> - Jonas
>>>
>>> Try with a QStyle:
>>>
>>> class MyTabWidget::TabStyle
>>>
>>>    : public QCommonStyle
>>>
>>> {
>>>
>>>  public:
>>>    TabStyle () {}
>>>    virtual ~TabStyle () {}
>>>
>>>    void drawControl (ControlElement el,
>>>          const QStyleOption * opt,
>>>          QPainter * p,
>>>          const QWidget * w) const
>>>    {
>>>       if (el == QStyle::CE_TabBarTab) {
>>>          const QStyleOptionTab * t = static_cast <const QStyleOptionTab
>>> *> (opt);
>>>          if (t) {
>>>             p->save ();
>>>             QStyleOptionTab tab (*t);
>>>
>>>             if (t->state.testFlag (QStyle::State_Selected)) {
>>>                if (isPrivate) {
>>>                   tab.palette.setColor (QPalette::WindowText, QColor
>>> (somecolor));
>>>                }
>>>                tab.palette.setBrush (QPalette::Window, QBrush
>>> (somebrush)); } else {
>>>                tab.palette.setColor (QPalette::WindowText, QColor
>>> (somecolor));
>>>                tab.palette.setBrush (QPalette::Button, somebrush);
>>>             }
>>>             QApplication::style ()->drawControl (el, &tab, p, w);
>>>             p->restore ();
>>>          } else {
>>>             QApplication::style ()->drawControl (el, opt, p, w);
>>>          }
>>>       } else {
>>>          QApplication::style ()->drawControl (el, opt, p, w);
>>>       }
>>>    }
>>> };
>>>
>>> Frank
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at qt.nokia.com
>>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
> 
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110804/63bc7f81/attachment.bin 


More information about the Qt-interest-old mailing list