【问题标题】:QTabBar tab size doesn't scale with stylesheet fontQTabBar 选项卡大小不随样式表字体缩放
【发布时间】:2014-09-25 18:07:32
【问题描述】:

我有以下样式表:

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;
 }

【问题讨论】:

    标签: c++ qt qtstylesheets


    【解决方案1】:

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

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

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

    【讨论】:

    • 在 QTabBar 而不是 QTabBar::tab 中设置字体是缺少的链接。它确实在样式表中起作用。
    • 谢谢,因为 ::tab 按预期使用字体,所以情况更糟。
    猜你喜欢
    • 2016-10-02
    • 2011-01-18
    • 1970-01-01
    • 2017-10-01
    • 2023-03-22
    • 2023-03-22
    • 2021-08-22
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多