QTabWidget with side shadows
Jump to navigation
Jump to search
[toc align_right="yes" depth="4"]
TabWidget
This TabWidget class provides side shadows for its tabs.
TabWidget.h
#ifndef TABWIDGET_H<br />#define TABWIDGET_H
#include <QtGui/QTabWidget&gt;<br />class QShowEvent;
class TabWidget : public QTabWidget<br />{<br /> Q_OBJECT
QWidget* leftShadow_;<br /> QWidget* rightShadow_;
void showEvent( QShowEvent* event );
public:<br /> explicit TabWidget( QWidget* parent = 0 );
private slots:<br /> void tabChanged( int tabNumber );<br />};
#endif // TABWIDGET_H<br />
TabWidget.cpp
#include "TabWidget.h&quot;<br />#include <QtGui/QTabBar&gt;
TabWidget::TabWidget( QWidget* parent ) :<br /> QTabWidget( parent ),<br /> leftShadow_( new QWidget( this ) ),<br /> rightShadow_( new QWidget( this ) )<br />{<br /> connect( this,<br /> SIGNAL (currentChanged( int ) ),<br /> SLOT( tabChanged( int ) ) );<br /> leftShadow_<s>>setObjectName( "leftshadow&quot; );<br /> leftShadow_</s>>setFixedWidth( 10 );<br /> leftShadow_<s>>setFixedHeight( tabBar()</s>>height() );<br /> rightShadow_<s>>setObjectName( "rightshadow&quot; );<br /> rightShadow_</s>>setFixedWidth( 10 );<br /> rightShadow_<s>>setFixedHeight( tabBar()</s>>height() );<br />}
void TabWidget::tabChanged( int tabNumber )<br />{<br /> const QTabBar* tabBar = this->tabBar();<br /> const QRect tabRect = tabBar->tabRect( tabNumber );<br /> if( tabNumber 0 )
&#123;
leftShadow_-&gt;hide();
&#125;
else
&#123;
QPoint leftPos = tabBar-&gt;mapTo( this,
tabRect.bottomLeft() );
leftShadow_-&gt;setFixedHeight( tabRect.height() - 4 ); // &#39;4&#39; is the difference between active and inactive tabs heights
leftShadow_-&gt;move( leftPos.x() - leftShadow_-&gt;width(),
leftPos.y() - leftShadow_-&gt;height() + 1 );
leftShadow_-&gt;show();
&#125;
if( tabNumber ( count() - 1 ) )<br /> {<br /> rightShadow_<s>>hide();<br /> }<br /> else<br /> {<br /> QPoint rightPos = tabBar</s>>mapTo( this,<br /> tabRect.bottomRight() );<br /> rightShadow_<s>>setFixedHeight( tabRect.height()</s> 4 );<br /> rightShadow_<s>>move( rightPos.x(),<br /> rightPos.y()</s> rightShadow_<s>>height() + 1 );<br /> rightShadow_</s>>show();<br /> }<br />}
void TabWidget::showEvent ( QShowEvent * event )<br />{<br /> Q_UNUSED( event )<br /> tabChanged( currentIndex() );<br />}<br />
Stylesheet for shadows
#leftshadow<br />{<br /> background-color: qlineargradient(spread:pad,x1:0.312,y1:0.419545,x2:1,y2:0,stop:0 rgba(0, 0, 0, 0),stop:0.817778 rgba(80, 80, 80, 193),stop:1 rgba(80, 80, 80, 255));<br />}<br />#rightshadow<br />{<br /> background-color: qlineargradient(spread:pad,x1:0.680,y1:0.419545,x2:0,y2:0,stop:0 rgba(0, 0, 0, 0),stop:0.817778 rgba(80, 80, 80, 193),stop:1 rgba(80, 80, 80, 255));<br />}