Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QTabWidget style issue with adwaita #145

Closed
jonaski opened this issue Nov 10, 2020 · 1 comment
Closed

QTabWidget style issue with adwaita #145

jonaski opened this issue Nov 10, 2020 · 1 comment

Comments

@jonaski
Copy link

jonaski commented Nov 10, 2020

I'm using a custom QTabWidget similar to the one in Qt Creator that have different modes, the default one is a vertical tab widget similar to the one you see in Qt Creator.
There is a style issue with adwaita where the tabs are stretched from top to bottom, empty space between icons and text.
I've found that this is caused by the height in subElementRect() for QStyle::SE_TabWidgetTabBar is returning the height of the whole tabwidget, instead of the height of the contents (all tabs) like Fusion and other Qt styles including the KDE ones.

With adwaita it looks like this:

image

With all other Qt styles it looks correct like this:

image

I made a workaround for awaita by overriding subElementRect() using QCommonStyle instead.

Here is the code including the override I did specifically for awaita.

class FancyTabWidgetProxyStyle : public QProxyStyle {
 public:
  explicit FancyTabWidgetProxyStyle(QStyle *style) : QProxyStyle(style), common_style_(new QCommonStyle()) {}
  ~FancyTabWidgetProxyStyle() override { common_style_->deleteLater(); }

  QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget = nullptr) const override {
    if (element == QStyle::SE_TabWidgetTabBar) {
      QRect proxy_style_rect = QProxyStyle::subElementRect(element, option, widget);
      // Fix stretched tabbar (adwaita style issue).
      proxy_style_rect.setHeight(common_style_->subElementRect(element, option, widget).height());
      return proxy_style_rect;
    }
    else {
      return QProxyStyle::subElementRect(element, option, widget);
    }
  }

 private:
  QCommonStyle *common_style_;
};

FancyTabWidget::FancyTabWidget(QWidget *parent) : QTabWidget(parent),
      style_(nullptr),
      menu_(nullptr),
      mode_(Mode_None),
      bottom_widget_(nullptr),
      bg_color_system_(true),
      bg_gradient_(true),
      iconsize_smallsidebar_(FancyTabWidget::IconSize_SmallSidebar),
      iconsize_largesidebar_(FancyTabWidget::IconSize_LargeSidebar)
  {

  FancyTabBar *tabBar = new FancyTabBar(this);
  setTabBar(tabBar);
  setTabPosition(QTabWidget::West);
  setMovable(true);
  setElideMode(Qt::ElideNone);
  setUsesScrollButtons(true);
  if (QApplication::style() && QApplication::style()->objectName().toLower().contains(QRegularExpression("^adwaita.*$"))) {
    style_ = new FancyTabWidgetProxyStyle(style());
    setStyle(style_);
  }

  connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));

}

FancyTabWidget::~FancyTabWidget() {
  if (style_) style_->deleteLater();
}


The full code is on GitHub here:

https://github.com/strawberrymusicplayer/strawberry/blob/master/src/widgets/fancytabwidget.cpp

To test without the hack, remove setStyle(style_); in the FancyTabWidget constructor.

@grulja
Copy link
Collaborator

grulja commented May 20, 2021

Fixed with 1a089c8. However, tabs in Adwaita-qt need major changes in future as they don't really look like Adwaita ones.

@grulja grulja closed this as completed May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants