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

Nic*_*aus 5 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;
}
Run Code Online (Sandbox Code Playgroud)

如果我没有在样式表中声明字体,则选项卡的大小将根据其包含的文本进行调整,但是,当我增加字体大小时,选项卡的大小将保持不变,并且文本会被截断。我已经尝试了所有宽度设置,但是我希望选项卡的宽度能够缩放到它包含的内容。

有人知道解决方法或解决方法吗?

我将样式表文件作为皮肤加载到程序中,因此,如果存在程序设计解决方案,则我更喜欢样式表解决方案

编辑:

这是具有适当标签尺寸的工作版本

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;
 }
Run Code Online (Sandbox Code Playgroud)

jus*_*gel 2

然后从 QTabBar 设置字体。下面是粗略的伪代码。

font = tabbar.font()
font.setPointSize(12)
font.setBold(true)
tabbar.setFont(font)
Run Code Online (Sandbox Code Playgroud)

您应该能够从 QTabWidget 访问 QTabBar,并且只需设置不带字体的样式表。我希望这能有所帮助。

  • 在 QTabBar 而不是 QTabBar::tab 中设置字体是缺少的链接。它确实在样式表中起作用。 (6认同)