How can I change the close button icon on a QTabWidget tab depending on the tab state?

Viewed 14

I am trying to come up with our own design for QTabWidget. I subclassed QTabWidget just so we keep consistency across our application.

The design is shown in expected behavior, however, what I got from my *.qss file is found in actual behavior. My *.qss file follows below, the variables starting with @ are substituted by a script where we set all the colors that we use on our software. The real question I have is: is there a way that I set the close-button icon (X) depending on the state of the tab (enabled/disabled)?

I also already tried using a QProxyStyle with no success (code snippet bellow as well).

GTabWidget QTabBar::tab {
    background-color:@gray20;
    color:@gray90;
    margin-right:4px;
    padding-right:16px;
    padding-left:16px;
    padding-bottom:4px;
    padding-top:4px;
    border-top-left-radius:4px;
    border-top-right-radius:4px;
}

GTabWidget QTabBar::tab:selected, GTabWidget QTabBar::tab:selected:hover {
    background-color:@gray20;
    border-bottom-width:4px;
    border-bottom-style:solid;
    border-bottom-color:@primary;
    padding-bottom:2px;
}

GTabWidget QTabBar::tab:!selected {
    background-color:@gray10;
    color:@gray80;
}

GTabWidget QTabBar::tab:!selected:hover {
    background-color:@gray20;
    color:@gray90;
    border-bottom-width:1px;
    border-bottom-style:solid;
    border-bottom-color:@primary;
    padding-bottom:3px;
}

GTabWidget QTabBar::tab:disabled {
    background-color:@gray30;
    color:@white;
}

GTabWidget QTabBar::close-button {
    image:url(:/icons/cross-gray90);
    padding:4px;
    background-color:transparent;
    border-radius:4px;
    margin-right:4px;
}

GTabWidget QTabBar::close-button:hover {
    border:1px solid @gray50;
}

GTabProxyStyle::GTabProxyStyle(QStyle* style)
    : QProxyStyle(style) {}

void GTabProxyStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption* option, QPainter* painter,
    const QWidget* widget) const {
  qDebug() << Q_FUNC_INFO;
  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(":/icons/cross-gray90").pixmap(size, mode, state);
    proxy()->drawItemPixmap(painter, option->rect, Qt::AlignCenter, pixmap);
  }
  else
  {
    QProxyStyle::drawPrimitive(element,option,painter,widget);
  }
}
0 Answers
Related