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

Jake Colman colman at ppllc.com
Thu Aug 4 17:18:19 CEST 2011


Jonas,

Never mind, stupid question.

I need to call setTabBar() with my ColorTabBar in order to have the
QTabWidget make use of it.

In my derived pqTabWidget constructor, I now have the following code:

  ColorTabBar* tabBar = new ColorTabBar( this );
  tabBar->setObjectName( QLatin1String("ColorTabBar") );
  tabBar->setDrawBase( false );
  this->setTabBar( tabBar );

This code was partially lifted from the QTabWidget's init() method.  

Everything is now working exactly as I requested.

Thanks again.

...Jake



>>>>> "Jake" == Jake Colman <colman at ppllc.com> writes:

   Jake> Jonas,

   Jake> Wow!  Thanks for an amazingly detailed solution to my problem.
   Jake> And you screen shot does exactly reflect what I am tying to do.

   Jake> I'm about to implement this but want to make sure that I am not
   Jake> missing anything.  I have overridden the QTabWidget::tabBar()
   Jake> so that it does the following:

   Jake> pqTabBar* pqTabWidget::tabBar() const
   Jake> {
   Jake>   return new ColorTabBar( this->QTabWidget::tabBar() );
   Jake> }

   Jake> Is this what you intended?  That I create ColorTabBar by
   Jake> constructing it from the QTabBar returned by
   Jake> QTabWidget::tabBar() method?

   Jake> ...Jake

>>>>> "JG" == Jonas Gehring <jonas.gehring at boolsoft.org> writes:

   JG> Hi Jake,

   JG> Here's a short example for a custom QTabBar implementation. You
   JG> need to subclass QTabWidget in order to use it.

   JG> class ColorTabBar : public QTabBar
   JG> {
   JG> public:
   JG> ColorTabBar(QWidget *parent = 0) : QTabBar(parent) { }

   JG> void setTabColor(int index, const QColor &color) {
   JG> m_colors[index] = color;
   JG> }

   JG> protected:
   JG> void paintEvent(QPaintEvent *event) {
   JG> QStylePainter p(this);
   JG> for (int i = 0; i < count(); i++) {
   JG> QStyleOptionTabV3 option;
   JG> QTabBar::initStyleOption(&option, i);

   JG> // Apply custom background color
   JG> if (m_colors.contains(i)) {
   JG> option.palette.setColor(backgroundRole(),
   JG> m_colors[i]);
   JG> }

   JG> p.drawControl(QStyle::CE_TabBarTab, option);
   JG> }
   JG> }

   JG> private:
   JG> QMap<int, QColor> m_colors;
   JG> };

   JG> This should illustrate the basic idea. You should take a look at
   JG> QTabBar's
   JG> implementation for corner case handling, though:
   JG> http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/widgets/qtabbar.cpp#line1526

   JG> - Jonas

   JG> On 08/04/2011 04:18 AM, Jake Colman wrote:
   >>> 
   >>> All I need (it sounds like it _should_ be simple) is, for a given tab
   >>> index, specify the background color based on certain runtime
   >>> conditions.  I have no problems subclassing specific Qt classes if
   >>> that's what's required.  Can you give me some more details as to how I
   >>> should go about doing this?
   >>> 
   >>>>>>>> "JG" == Jonas Gehring <jonas.gehring at boolsoft.org> writes:
   >>> 
   JG> On 08/04/2011 12:13 AM, Scott Aron Bloom wrote:
   >>> >> Yes, CSS updates at runtime...
   >>> 
   JG> True, but I'm afraid you can't explicitly set the background
   JG> color of tab i. According to the documentation at
   JG> http://doc.qt.nokia.com/latest/stylesheet-reference.html#qtabbar-widget
   JG> , you can only manipulate a few specific tabs using style sheets
   JG> (:first, :last, ...).
   >>> 
   JG> You can reimplement QTabBar::paintEvent() and modify the
   JG> QStyleOptionTabV3 structure, but you've got to be careful not to
   JG> mess up with desktop integration.
   >>> 
   JG> - Jonas
   >>> 
   >>> >> -----Original Message-----
   >>> >> From: qt-interest-bounces+scott.bloom=onshorecs.com at qt.nokia.com
   >>> >> [mailto:qt-interest-bounces+scott.bloom=onshorecs.com at qt.nokia.com]
   >>> >> On
   >>> >> Behalf Of Jake Colman
   >>> >> Sent: Wednesday, August 03, 2011 1:04 PM
   >>> >> To: qt-interest at qt.nokia.com
   >>> >> Subject: Re: [Qt-interest] Setting the background color of a tab
   >>> >> 
   >>> >> 
   >>> >> 
   >>> >> I need to set it on the fly at runtime as things change.  And I need
   >>> >> different tabs to have different background colors.  Can all that be
   >>> >> done via CSS?
   JG> _______________________________________________
   JG> Qt-interest mailing list
   JG> Qt-interest at qt.nokia.com
   JG> http://lists.qt.nokia.com/mailman/listinfo/qt-interest

   Jake> -- 
   Jake> Jake Colman | Director, Software Development
   Jake> Principia Partners LLC
   Jake> 101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755 9770
   Jake> t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221 8929
   Jake> e: colman at ppllc.com | w: www.principiapartners.com
   Jake> Credit technology innovation awards winner 2008, 2009 & 2010

-- 
Jake Colman | Director, Software Development
Principia Partners LLC
101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755 9770
t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221 8929
e: colman at ppllc.com | w: www.principiapartners.com
Credit technology innovation awards winner 2008, 2009 & 2010




More information about the Qt-interest-old mailing list