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

Jonas Gehring jonas.gehring at boolsoft.org
Thu Aug 4 10:23:55 CEST 2011


Hi Jake,

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

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

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

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

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

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

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

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

- Jonas


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?

-------------- 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/f97e3954/attachment.bin 


More information about the Qt-interest-old mailing list