Override QStyle::subElementRect in fancy tabbar to fix style problems

Something is causing the contents of the tabbar to be stretched from top to bottom with space between icons and text.
You can see this on the default Fedora (Gnome) installation.
Also fixes the tabbar on macOS where the content was in the middle instead of the top.
This commit is contained in:
Jonas Kvinge
2020-10-31 14:05:06 +01:00
parent 4804a05736
commit cabd6e6e9d
2 changed files with 73 additions and 44 deletions

View File

@@ -52,6 +52,11 @@
#include <QLayout>
#include <QBoxLayout>
#include <QtEvents>
#include <QStyle>
#include <QCommonStyle>
#include <QProxyStyle>
#include <QStyleOption>
#include <QStyleOptionComplex>
#include "fancytabwidget.h"
#include "core/stylehelper.h"
@@ -406,7 +411,25 @@ void FancyTabWidget::currentTabChanged(const int idx) {
}
FancyTabWidget::FancyTabWidget(QWidget* parent) : QTabWidget(parent),
// Override subElementRect() and use QCommonStyle to fix a problem with certain styles.
// Something is causing the contents of the tabbar to be stretched from top to bottom with space between icons and text.
// You can see this on the default Fedora (Gnome) installation.
// Also fixes the tabbar on macOS where the content was in the middle.
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 {
return common_style_->subElementRect(element, option, widget);
}
private:
QCommonStyle *common_style_;
};
FancyTabWidget::FancyTabWidget(QWidget *parent) : QTabWidget(parent),
menu_(nullptr),
mode_(Mode_None),
bottom_widget_(nullptr),
@@ -422,11 +445,16 @@ FancyTabWidget::FancyTabWidget(QWidget* parent) : QTabWidget(parent),
setMovable(true);
setElideMode(Qt::ElideNone);
setUsesScrollButtons(true);
setStyle(new FancyTabWidgetProxyStyle(style()));
connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
}
FancyTabWidget::~FancyTabWidget() {
style()->deleteLater();
}
void FancyTabWidget::Load(const QString &kSettingsGroup) {
QSettings s;