原生的QTabBar放上图标和文字,固定QTab的宽度时,间距较大。通过自定义样式,轻松解决间距问题
cpp
#include <QProxyStyle>
class MyTabStyle : public QProxyStyle
{
Q_OBJECT
public:
explicit MyTabStyle (QStyle *style = nullptr) : QProxyStyle(style)
{
}
virtual void drawItemText(QPainter *painter, const QRect &rect,
int flags, const QPalette &pal, bool enabled,
const QString &text,
QPalette::ColorRole textRole) const Q_DECL_OVERRIDE
{
QStyle::drawItemText(painter, rect, Qt::AlignLeft | Qt::AlignVCenter,
pal, enabled, text, textRole);
}
};
应用示例:
cpp
ui->tabWidget->tabBar()->setStyle(new MyTabBar);