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

Frank Hemer frank at hemer.org
Thu Aug 4 12:43:03 CEST 2011


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





More information about the Qt-interest-old mailing list