QTabWidget with side shadows: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(clean-up) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
This TabWidget class provides side shadows for its tabs. | This TabWidget class provides side shadows for its tabs. | ||
==== TabWidget.h ==== | ==== TabWidget.h ==== | ||
<code>#ifndef TABWIDGET_H | <code>#ifndef TABWIDGET_H | ||
#define TABWIDGET_H | |||
#include | #include <QTabWidget> | ||
class QShowEvent; | |||
class TabWidget : public QTabWidget | class TabWidget : public QTabWidget | ||
{ | |||
Q_OBJECT | |||
QWidget* leftShadow_; | QWidget* leftShadow_; | ||
QWidget* rightShadow_; | |||
void showEvent( QShowEvent* event ); | void showEvent( QShowEvent* event ); | ||
public: | public: | ||
explicit TabWidget( QWidget* parent = 0 ); | |||
private slots: | private slots: | ||
void tabChanged( int tabNumber ); | |||
}; | |||
#endif // TABWIDGET_H | #endif // TABWIDGET_H | ||
</code> | |||
==== TabWidget.cpp ==== | ==== TabWidget.cpp ==== | ||
<code>#include | <code>#include "TabWidget.h" | ||
#include <QTabBar> | |||
TabWidget::TabWidget( QWidget* parent ) : | TabWidget::TabWidget( QWidget* parent ) : | ||
QTabWidget( parent ), | |||
leftShadow_( new QWidget( this ) ), | |||
rightShadow_( new QWidget( this ) ) | |||
{ | |||
connect( this, | |||
SIGNAL (currentChanged( int ) ), | |||
SLOT( tabChanged( int ) ) ); | |||
leftShadow_->setObjectName( "leftshadow" ); | |||
leftShadow_->setFixedWidth( 10 ); | |||
leftShadow_->setFixedHeight( tabBar()->height() ); | |||
rightShadow_->setObjectName( "rightshadow" ); | |||
rightShadow_->setFixedWidth( 10 ); | |||
rightShadow_->setFixedHeight( tabBar()->height() ); | |||
} | |||
void TabWidget::tabChanged( int tabNumber ) | void TabWidget::tabChanged( int tabNumber ) | ||
{ | |||
leftShadow_- | const QTabBar* tabBar = this->tabBar(); | ||
const QRect tabRect = tabBar->tabRect( tabNumber ); | |||
if( tabNumber 0 ) | |||
{ | |||
leftShadow_->hide(); | |||
} | |||
else | else | ||
{ | |||
QPoint leftPos = tabBar- | QPoint leftPos = tabBar->mapTo( this, | ||
tabRect.bottomLeft() ); | tabRect.bottomLeft() ); | ||
leftShadow_- | leftShadow_->setFixedHeight( tabRect.height() - 4 ); // '4' is the difference between active and inactive tabs heights | ||
leftShadow_- | leftShadow_->move( leftPos.x() - leftShadow_->width(), | ||
leftPos.y() - leftShadow_- | leftPos.y() - leftShadow_->height() + 1 ); | ||
leftShadow_- | leftShadow_->show(); | ||
} | |||
if( tabNumber ( count() - 1 ) ) | if( tabNumber ( count() - 1 ) ) | ||
{ | |||
rightShadow_->hide(); | |||
} | |||
else | |||
{ | |||
QPoint rightPos = tabBar->mapTo( this, | |||
tabRect.bottomRight() ); | |||
rightShadow_->setFixedHeight( tabRect.height()- 4 ); | |||
rightShadow_->move( rightPos.x(), | |||
rightPos.y()- rightShadow_->height() + 1 ); | |||
rightShadow_->show(); | |||
} | |||
} | |||
void TabWidget::showEvent ( QShowEvent * event ) | void TabWidget::showEvent ( QShowEvent * event ) | ||
{ | |||
Q_UNUSED( event ) | |||
tabChanged( currentIndex() ); | |||
} | |||
</code> | |||
==== Stylesheet for shadows ==== | ==== Stylesheet for shadows ==== | ||
<code>#leftshadow | <code>#leftshadow | ||
{ | |||
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)); | |||
} | |||
#rightshadow | |||
{ | |||
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)); | |||
} | |||
</code> |
Latest revision as of 11:28, 24 March 2016
This TabWidget class provides side shadows for its tabs.
TabWidget.h
#ifndef TABWIDGET_H
#define TABWIDGET_H
#include <QTabWidget>
class QShowEvent;
class TabWidget : public QTabWidget
{
Q_OBJECT
QWidget* leftShadow_;
QWidget* rightShadow_;
void showEvent( QShowEvent* event );
public:
explicit TabWidget( QWidget* parent = 0 );
private slots:
void tabChanged( int tabNumber );
};
#endif // TABWIDGET_H
TabWidget.cpp
#include "TabWidget.h"
#include <QTabBar>
TabWidget::TabWidget( QWidget* parent ) :
QTabWidget( parent ),
leftShadow_( new QWidget( this ) ),
rightShadow_( new QWidget( this ) )
{
connect( this,
SIGNAL (currentChanged( int ) ),
SLOT( tabChanged( int ) ) );
leftShadow_->setObjectName( "leftshadow" );
leftShadow_->setFixedWidth( 10 );
leftShadow_->setFixedHeight( tabBar()->height() );
rightShadow_->setObjectName( "rightshadow" );
rightShadow_->setFixedWidth( 10 );
rightShadow_->setFixedHeight( tabBar()->height() );
}
void TabWidget::tabChanged( int tabNumber )
{
const QTabBar* tabBar = this->tabBar();
const QRect tabRect = tabBar->tabRect( tabNumber );
if( tabNumber 0 )
{
leftShadow_->hide();
}
else
{
QPoint leftPos = tabBar->mapTo( this,
tabRect.bottomLeft() );
leftShadow_->setFixedHeight( tabRect.height() - 4 ); // '4' is the difference between active and inactive tabs heights
leftShadow_->move( leftPos.x() - leftShadow_->width(),
leftPos.y() - leftShadow_->height() + 1 );
leftShadow_->show();
}
if( tabNumber ( count() - 1 ) )
{
rightShadow_->hide();
}
else
{
QPoint rightPos = tabBar->mapTo( this,
tabRect.bottomRight() );
rightShadow_->setFixedHeight( tabRect.height()- 4 );
rightShadow_->move( rightPos.x(),
rightPos.y()- rightShadow_->height() + 1 );
rightShadow_->show();
}
}
void TabWidget::showEvent ( QShowEvent * event )
{
Q_UNUSED( event )
tabChanged( currentIndex() );
}
Stylesheet for shadows
#leftshadow
{
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));
}
#rightshadow
{
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));
}