QTabWidget和QTabBar的外观定制

转自http://developer.qt.nokia.com/doc/qt-4.8/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar

Customizing QTabWidget and QTabBar

For the screenshot above, we need a stylesheet as follows:

 QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
 }

 QTabWidget::tab-bar {
     left: 5px; /* move to the right by 5px */
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

 QTabBar::tab:!selected {
     margin-top: 2px; /* make non-selected tabs look smaller */
 }

Often we require the tabs to overlap to look like below:

For a tab widget that looks like above, we make use of negative margins. The resulting stylesheet looks like this:

 QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
 }

 QTabWidget::tab-bar {
     left: 5px; /* move to the right by 5px */
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

 QTabBar::tab:!selected {
     margin-top: 2px; /* make non-selected tabs look smaller */
 }

 /* make use of negative margins for overlapping tabs */
 QTabBar::tab:selected {
     /* expand/overlap to the left and right by 4px */
     margin-left: -4px;
     margin-right: -4px;
 }

 QTabBar::tab:first:selected {
     margin-left: 0; /* the first selected tab has nothing to overlap with on the left */
 }

 QTabBar::tab:last:selected {
     margin-right: 0; /* the last selected tab has nothing to overlap with on the right */
 }

 QTabBar::tab:only-one {
     margin: 0; /* if there is only one tab, we don't want overlapping margins */
 }

To move the tab bar to the center (as below), we require the following stylesheet:

 QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
     position: absolute;
     top: -0.5em;
 }

 QTabWidget::tab-bar {
     alignment: center;
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                 stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
     border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

The tear indicator and the scroll buttons can be further customized as follows:

 QTabBar::tear {
     image: url(tear_indicator.png);
 }

 QTabBar::scroller { /* the width of the scroll buttons */
     width: 20px;
 }

 QTabBar QToolButton { /* the scroll buttons are tool buttons */
     border-image: url(scrollbutton.png) 2;
     border-width: 2px;
 }

 QTabBar QToolButton::right-arrow { /* the arrow mark in the tool buttons */
     image: url(rightarrow.png);
 }

 QTabBar QToolButton::left-arrow {
     image: url(leftarrow.png);
 }

Since Qt 4.6 the close button can be customized as follow:

 QTabBar::close-button {
     image: url(close.png)
     subcontrol-position: left;
 }
 QTabBar::close-button:hover {
     image: url(close-hover.png)
 }
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
QTabWidgetQTabBar是两个常用的Qt控件,可以用于实现选项卡式界面。下面是一个简单的示例,演示如何使用QTabWidgetQTabBar控件。 首先,创建一个QMainWindow窗口,并在其中添加一个QTabWidget控件。然后,使用QTabWidget的addTab方法添加标签页,可以使用QWidget或其他控件作为标签页的内容。 ```cpp QMainWindow* mainWindow = new QMainWindow(); QTabWidget* tabWidget = new QTabWidget(mainWindow); QWidget* tab1 = new QWidget(); tabWidget->addTab(tab1, "Tab 1"); QWidget* tab2 = new QWidget(); tabWidget->addTab(tab2, "Tab 2"); mainWindow->setCentralWidget(tabWidget); mainWindow->show(); ``` 这段代码创建了一个包含两个标签页的选项卡窗口。现在,要将QTabWidget中的选项卡标签显示为QTabBar。 ```cpp QTabBar* tabBar = tabWidget->tabBar(); tabBar->setTabsClosable(true); tabBar->setMovable(true); ``` 这段代码获取QTabWidgetQTabBar控件,并设置选项卡标签可关闭和可移动。可以使用setTabText方法更改选项卡标签的文本。 ```cpp tabBar->setTabText(0, "First Tab"); tabBar->setTabText(1, "Second Tab"); ``` 这段代码将标签页的文本更改为"First Tab"和"Second Tab"。 下面是完整的示例代码: ```cpp #include <QApplication> #include <QMainWindow> #include <QTabWidget> #include <QTabBar> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow* mainWindow = new QMainWindow(); QTabWidget* tabWidget = new QTabWidget(mainWindow); QWidget* tab1 = new QWidget(); tabWidget->addTab(tab1, "Tab 1"); QWidget* tab2 = new QWidget(); tabWidget->addTab(tab2, "Tab 2"); QTabBar* tabBar = tabWidget->tabBar(); tabBar->setTabsClosable(true); tabBar->setMovable(true); tabBar->setTabText(0, "First Tab"); tabBar->setTabText(1, "Second Tab"); mainWindow->setCentralWidget(tabWidget); mainWindow->show(); return app.exec(); } ``` 这个示例演示了如何使用QTabWidgetQTabBar控件创建一个选项卡式界面,并设置选项卡标签的属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值