在qt中自定义样式表中的选项卡控件(customizing tab control in stylesheet in qt)

编程入门 行业动态 更新时间:2024-10-20 16:33:47
在qt中自定义样式表中的选项卡控件(customizing tab control in stylesheet in qt)

我想仅针对特定选项卡进行更改。 我该怎么做?

我试过了两个:

QTabBar::tab#tbGeneral{... } QTabWidget::tab-bar#tbGeneral{... }

没有用。

I would like to do changes only for a specific tab. How can I do it?

I tried both:

QTabBar::tab#tbGeneral{... } QTabWidget::tab-bar#tbGeneral{... }

None worked.

最满意答案

您可能已经研究过Customizing QTabWidget和QTabBar 。

要根据各个选项卡的状态设置样式(:only-one,:first,:last,:middle,:previous-selected,:next-selected,:selected),您可以使用与此类似的样式表代码:

QTabBar::tab { border: 1px solid #C4C4C3; border-bottom-color: #C2C7CB; border-top-left-radius: 4px; border-top-right-radius: 4px; padding: 1px 3px; margin-left: 1px; margin-bottom: 4px; } QTabBar::tab:selected { background-color: #f14040; border-bottom-style: none; }

由于各个选项卡不是窗口小部件(或对象),因此它们没有对象名称或其他可以将它们标识到样式表的属性。 您只能使用伪类来设置样式表的样式。

您可能必须使用(C ++)代码来根据标签更改选项卡的样式。 在Qt中自定义样式的推荐方法是通过类QStyle 。 您可以子类化QStyle或使用QProxyStyle来更改特定小部件的外观。 另一个替代方案(可能不是Qt推荐的)是通过继承QTabBar并重新实现函数QWidget::paintEvent( QPaintEvent *event) 。

You have probably looked into Customizing QTabWidget and QTabBar.

To style individual tabs based on their state (:only-one, :first, :last, :middle, :previous-selected, :next-selected, :selected) you can use stylesheet-code similar to this:

QTabBar::tab { border: 1px solid #C4C4C3; border-bottom-color: #C2C7CB; border-top-left-radius: 4px; border-top-right-radius: 4px; padding: 1px 3px; margin-left: 1px; margin-bottom: 4px; } QTabBar::tab:selected { background-color: #f14040; border-bottom-style: none; }

As the individual tabs are not widgets (or objects), they have no object name or other properties that could identify them to a stylsheet. You can only use the pseudo-classes to style tabs with a stylesheet.

You'll probably have to use (C++) code to change the style of a tab depending on the label. The recommended way for customizing styles in Qt is through the class QStyle. You can either subclass QStyle or use QProxyStyle to change the looks of specific widgets. The other alternative (probably not recommended by Qt) is though subclassing QTabBar and reimplementing the function QWidget::paintEvent( QPaintEvent *event).

更多推荐

本文发布于:2023-07-06 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   选项卡   样式表   qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!