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

Jake Colman colman at ppllc.com
Thu Aug 4 15:23:46 CEST 2011


Jonas,

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

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

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

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

...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 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 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