Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

  #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()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top