QTabWidget with side shadows: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=TabWidget=
[[Category:snippets]][toc align_right="yes" depth="4"]
 
= TabWidget =


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<br />#define TABWIDGET_H
 
#include &lt;QtGui/QTabWidget&amp;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 /></code>
 
==== TabWidget.cpp ====
 
<code>#include &quot;TabWidget.h&amp;quot;<br />#include &lt;QtGui/QTabBar&amp;gt;


====TabWidget.cpp====
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>&gt;setObjectName( &quot;leftshadow&amp;quot; );<br /> leftShadow_</s>&gt;setFixedWidth( 10 );<br /> leftShadow_<s>&gt;setFixedHeight( tabBar()</s>&gt;height() );<br /> rightShadow_<s>&gt;setObjectName( &quot;rightshadow&amp;quot; );<br /> rightShadow_</s>&gt;setFixedWidth( 10 );<br /> rightShadow_<s>&gt;setFixedHeight( tabBar()</s>&gt;height() );<br />}


====Stylesheet for shadows====
void TabWidget::tabChanged( int tabNumber )<br />{<br /> const QTabBar* tabBar = this-&gt;tabBar();<br /> const QRect tabRect = tabBar-&gt;tabRect( tabNumber );<br /> if( tabNumber  0 )
    &amp;#123;
        leftShadow_-&amp;gt;hide();
    &amp;#125;
    else
    &amp;#123;
        QPoint leftPos = tabBar-&amp;gt;mapTo( this,
                                        tabRect.bottomLeft() );
        leftShadow_-&amp;gt;setFixedHeight( tabRect.height() - 4 ); // &amp;#39;4&amp;#39; is the difference between active and inactive tabs heights
        leftShadow_-&amp;gt;move( leftPos.x() - leftShadow_-&amp;gt;width(),
                          leftPos.y() - leftShadow_-&amp;gt;height() + 1  );
        leftShadow_-&amp;gt;show();
    &amp;#125;
    if( tabNumber  ( count() - 1 ) )<br /> {<br /> rightShadow_<s>&gt;hide();<br /> }<br /> else<br /> {<br /> QPoint rightPos = tabBar</s>&gt;mapTo( this,<br /> tabRect.bottomRight() );<br /> rightShadow_<s>&gt;setFixedHeight( tabRect.height()</s> 4 );<br /> rightShadow_<s>&gt;move( rightPos.x(),<br /> rightPos.y()</s> rightShadow_<s>&gt;height() + 1 );<br /> rightShadow_</s>&gt;show();<br /> }<br />}


====Example====
void TabWidget::showEvent ( QShowEvent * event )<br />{<br /> Q_UNUSED( event )<br /> tabChanged( currentIndex() );<br />}<br /></code>


[[Image:mainwindow001.png|Example]]
==== Stylesheet for shadows ====


===Categories:===
<code>#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 />}</code>


* [[:Category:snippets|snippets]]
==== Example ====

Revision as of 10:42, 24 February 2015

[toc align_right="yes&quot; depth="4&quot;]

TabWidget

This TabWidget class provides side shadows for its tabs.

TabWidget.h

#ifndef TABWIDGET_H<br />#define TABWIDGET_H

#include &lt;QtGui/QTabWidget&amp;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 &quot;TabWidget.h&amp;quot;<br />#include &lt;QtGui/QTabBar&amp;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>&gt;setObjectName( &quot;leftshadow&amp;quot; );<br /> leftShadow_</s>&gt;setFixedWidth( 10 );<br /> leftShadow_<s>&gt;setFixedHeight( tabBar()</s>&gt;height() );<br /> rightShadow_<s>&gt;setObjectName( &quot;rightshadow&amp;quot; );<br /> rightShadow_</s>&gt;setFixedWidth( 10 );<br /> rightShadow_<s>&gt;setFixedHeight( tabBar()</s>&gt;height() );<br />}

void TabWidget::tabChanged( int tabNumber )<br />{<br /> const QTabBar* tabBar = this-&gt;tabBar();<br /> const QRect tabRect = tabBar-&gt;tabRect( tabNumber );<br /> if( tabNumber  0 )
    &amp;#123;
        leftShadow_-&amp;gt;hide();
    &amp;#125;
    else
    &amp;#123;
        QPoint leftPos = tabBar-&amp;gt;mapTo( this,
                                        tabRect.bottomLeft() );
        leftShadow_-&amp;gt;setFixedHeight( tabRect.height() - 4 ); // &amp;#39;4&amp;#39; is the difference between active and inactive tabs heights
        leftShadow_-&amp;gt;move( leftPos.x() - leftShadow_-&amp;gt;width(),
                           leftPos.y() - leftShadow_-&amp;gt;height() + 1  );
        leftShadow_-&amp;gt;show();
    &amp;#125;
    if( tabNumber  ( count() - 1 ) )<br /> {<br /> rightShadow_<s>&gt;hide();<br /> }<br /> else<br /> {<br /> QPoint rightPos = tabBar</s>&gt;mapTo( this,<br /> tabRect.bottomRight() );<br /> rightShadow_<s>&gt;setFixedHeight( tabRect.height()</s> 4 );<br /> rightShadow_<s>&gt;move( rightPos.x(),<br /> rightPos.y()</s> rightShadow_<s>&gt;height() + 1 );<br /> rightShadow_</s>&gt;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 />}

Example