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

Frank Hemer frank at hemer.org
Thu Aug 4 11:52:16 CEST 2011


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



More information about the Qt-interest-old mailing list