QTabBar选项卡大小不随样式表字体缩放

时间:2014-09-25 18:07:33

标签: c++ qt qtstylesheets

我有以下样式表:

QTabBar::tab {

background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,
                        stop: 0 #2A2A2A, stop: 0.4 #E1E1E1,
                        stop: 0.5 #E1E1E1, stop: 1.0 #2A2A2A);
background-image: url(:/metal_toolbar);
border-left: 1px solid #9B9B9B;
border-right: 1px solid #9B9B9B;
border-bottom: 1px solid #9B9B9B;
border-top-color: #5A5A5A;
font: bold 12pt;
/*min-width: 20ex;
max-width: 1000ex;*/
padding: 2px;
}

如果我没有在样式表中声明字体,我的标签大小适合它们包含的文字,但是当我增加字体大小时,标签大小保持不变并且文本被截止。我已经尝试了所有宽度设置,但我希望标签宽度可以缩放到它所包含的内容。

任何人都知道解决方法或解决此问题吗?

我将样式表文件作为外观加载到我的程序中,所以如果存在样式解决方案,我更喜欢样式表解决方案

编辑:

这是具有适当标签尺寸

的工作版本
QTabBar
{
    font: bold 9pt;
}

 QTabBar::tab 
 {

    background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,
                            stop: 0 #2A2A2A, stop: 0.4 #E1E1E1,
                            stop: 0.5 #E1E1E1, stop: 1.0 #2A2A2A);
    background-image: url(:/metal_toolbar);
    border-left: 1px solid #9B9B9B;
    border-right: 1px solid #9B9B9B;
    border-bottom: 1px solid #9B9B9B;
    border-top-color: #5A5A5A;
    min-width: 20ex;
    padding: 2px;
 }

1 个答案:

答案 0 :(得分:2)

然后从QTabBar设置字体。粗糙的伪代码如下。

font = tabbar.font()
font.setPointSize(12)
font.setBold(true)
tabbar.setFont(font)

您应该可以从QTabWidget访问QTabBar,只需设置没有字体的样式表。我希望这可以提供帮助。