3

I am using QTabWidget, and I would like to know if I can use different icons for close buttons on tabs? I think style and setCornerWidget may not work for this case.

Thanks!

echo
  • 789
  • 2
  • 12
  • 21

5 Answers5

6

Use setStyleSheet() with

 QTabBar::close-button {
     image: url(close.png)
 }
 QTabBar::close-button:hover {
     image: url(close-hover.png)
 }

http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar

Paul Hutchinson
  • 1,578
  • 15
  • 21
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
3

I don't think that this is possible with a QTabWidget. You could use a QTabBar where you can use QTabBar::setTabButton to set a widget of your own design into the tab position.

hmuelner
  • 8,093
  • 1
  • 28
  • 39
1
#include <QProxyStyle>

class AppStyle : public QProxyStyle
{
    Q_OBJECT
public:
    AppStyle(QStyle *style = 0) : QProxyStyle(style) {}
    
    void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    {
        if (element == PE_IndicatorTabClose)
        {
            int size = proxy()->pixelMetric(QStyle::PM_SmallIconSize);
            QIcon::Mode mode = option->state & State_Enabled ?
            (option->state & State_Raised ? QIcon::Active : QIcon::Normal)
            : QIcon::Disabled;
            if (!(option->state & State_Raised)
                && !(option->state & State_Sunken)
                && !(option->state & QStyle::State_Selected))
                mode = QIcon::Disabled;
            
            QIcon::State state = option->state & State_Sunken ? QIcon::On : QIcon::Off;
            QPixmap pixmap = QIcon(":myclose.png").pixmap(size, mode, state);
            proxy()->drawItemPixmap(painter, option->rect, Qt::AlignCenter, pixmap);
        }
        else
        {
            QProxyStyle::drawPrimitive(element,option,painter,widget);
        }
    }
};

in main.cpp:

QApplication app(argc, argv);
app.setStyle(new AppStyle(app.style()));
Waqar
  • 8,558
  • 4
  • 35
  • 43
archy
  • 11
  • 1
0

The default close buttons on tabs are part of the QStyle you are using.

From the Qt sources:

    case PE_IndicatorTabClose: {
        if (d->tabBarcloseButtonIcon.isNull()) {
            d->tabBarcloseButtonIcon.addPixmap(QPixmap(
                        QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-16.png")),
                        QIcon::Normal, QIcon::Off);
            d->tabBarcloseButtonIcon.addPixmap(QPixmap(
                        QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-down-16.png")),
                        QIcon::Normal, QIcon::On);
            d->tabBarcloseButtonIcon.addPixmap(QPixmap(
                        QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-hover-16.png")),
                        QIcon::Active, QIcon::Off);
        }

From what it looks, you have to subclass QStyle and override PE_IndicatorTabClose and return a different QIcon path.

BastiBen
  • 19,679
  • 11
  • 56
  • 86
0

For instance, if you want to change the icon to one with transparent background, and change the background icon on hovering:

QTabBar::close-button
{
    image: url(:icons/close.svg)
}

QTabBar::close-button:hover
{
    background: #A0A0A0
}
mmerle
  • 451
  • 4
  • 10