[Interest] How to programatically make a tab's text bold?

Igor Mironchik igor.mironchik at gmail.com
Wed Apr 4 12:07:22 CEST 2018


#include <QTabWidget>
#include <QApplication>
#include <QTabBar>
#include <QProxyStyle>
#include <QPainter>
#include <QStyleOption>

class Style : public QProxyStyle
{
public:
     Style( QTabBar * bar )
         :    m_boldTab( 0 )
         ,    m_bar( bar )
     {
     }

     ~Style()
     {
     }

     void drawControl( ControlElement element,
         const QStyleOption *option,
         QPainter *painter,
         const QWidget *widget = Q_NULLPTR) const
     {
         if( element == QStyle::CE_TabBarTab )
         {
             if( m_bar->tabAt( option->rect.center() ) == m_boldTab )
             {
                 QFont font = widget->font();
                 font.setBold( true );

                 painter->save();
                 painter->setFont( font );
                 QProxyStyle::drawControl( element, option, painter, 
widget );
                 painter->restore();
             }
             else
                 QProxyStyle::drawControl( element, option, painter, 
widget );
         }
         else
             QProxyStyle::drawControl( element, option, painter, widget );
     }

private:
     int m_boldTab;
     QTabBar * m_bar;
};

class Tab : public QTabBar
{
public:
     Tab( QWidget * parent )
         :    QTabBar( parent )
         ,    m_boldTab( 0 )
     {
     }

     ~Tab()
     {
     }

protected:
     QSize tabSizeHint(int index) const
     {
         if( index == m_boldTab )
         {
             const QSize s = QTabBar::tabSizeHint( index );
             const QFontMetrics fm( font() );
             const int w = fm.width( tabText( index ) );

             QFont f = font();
             f.setBold( true );
             const QFontMetrics bfm( f );

             const int bw = bfm.width( tabText( index ) );

             return QSize( s.width() - w + bw, s.height() );
         }
         else
             return QTabBar::tabSizeHint( index );
     }

private:
     int m_boldTab;
};

class TabWidget
     :    public QTabWidget
{
public:
     TabWidget()
         :    m_tabBar( new Tab( this ) )
         ,    m_style( m_tabBar )
     {
         m_tabBar->setStyle( &m_style );
         setTabBar( m_tabBar );
     }

     ~TabWidget()
     {
     }

private:
     Tab * m_tabBar;
     Style m_style;
};

int main( int argc, char ** argv )
{
     QApplication app( argc, argv );

     TabWidget t;
     QWidget w1, w2;
     t.addTab( &w1, "Test Tab With Long Name" );
     t.addTab( &w2, "Second Tab" );

     t.show();

     return app.exec();
}


On 04.04.2018 10:47, Vadim Peretokin wrote:
>
> On Wed, Apr 4, 2018 at 7:35 AM Hamish Moffatt 
> <hamish at risingsoftware.com <mailto:hamish at risingsoftware.com>> wrote:
>
>     On 04/04/18 15:10, Vadim Peretokin wrote:
>     > How can I programatically make a tab's text bold? I'm looking to
>     > notify the user that they need to look at it.
>     >
>     > Note that this is different from using a stylesheet to set the
>     > selected tab bold - I'd like to set any random tab's text bold.
>     >
>     > Been looking at this for several days, appreciate any advice!
>
>     Using widgets? You didn't say what context.
>
>     Maybe you can set a dynamic property on the tab when you want it to be
>     bold, and attach a special style using properties in the stylesheet.
>
>
>     Hamish
>     _______________________________________________
>     Interest mailing list
>     Interest at qt-project.org <mailto:Interest at qt-project.org>
>     http://lists.qt-project.org/mailman/listinfo/interest
>
>
> Yes, using widgets.
>
> I've tried a dynamic property however as far as I can see, I can only 
> set the dynamic property on the entire tab bar - not a specific tab. 
> How can I set it on a specific tab?
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180404/5d5e6246/attachment.html>


More information about the Interest mailing list