QTabWidget改变tabBar位置 并改变文字方向
一、
二、
#ifndef CUSTOMTABBAR_H #define CUSTOMTABBAR_H #include <QStyleOptionTab> #include <QStylePainter> #include <QTabBar> class CustomTabBar:public QTabBar { public: CustomTabBar(); QSize tabSizeHint(int index) const override; protected: void paintEvent(QPaintEvent *) override; }; #endif // CUSTOMTABBAR_H
#include "customtabbar.h" CustomTabBar::CustomTabBar() { } QSize CustomTabBar::tabSizeHint(int index) const { QSize s = QTabBar::tabSizeHint(index); s.transpose(); return s; } void CustomTabBar::paintEvent(QPaintEvent *) { QStylePainter painter(this); QStyleOptionTab opt; for(int i = 0;i < count();i++) { initStyleOption(&opt,i); painter.drawControl(QStyle::CE_TabBarTabShape, opt); painter.save(); QSize s = opt.rect.size(); s.transpose(); QRect r(QPoint(), s); r.moveCenter(opt.rect.center()); opt.rect = r; QPoint c = tabRect(i).center(); painter.translate(c); painter.rotate(90); painter.translate(-c); painter.drawControl(QStyle::CE_TabBarTabLabel,opt); painter.restore(); } }
#ifndef KTABWIDGET_H #define KTABWIDGET_H #include <QWidget> #include <QTabWidget> #include "utility/customtabbar.h" class KTabWidget : public QTabWidget { Q_OBJECT public: explicit KTabWidget(QWidget *parent = nullptr); signals: }; #endif // KTABWIDGET_H
#include "ktabwidget.h" KTabWidget::KTabWidget(QWidget *parent) : QTabWidget(parent) { this->setTabBar(new CustomTabBar()); this->setTabPosition(QTabWidget::West); }
使用
KTabWidget *tabWidget=new KTabWidget(this); tabWidget->addTab(saIOMonitor,"IO监控"); tabWidget->addTab(saIOSet,"I/O设置"); tabWidget->addTab(saDIDOWidget,"DI/DO功能监视");