在 Qt 中使用 CSS 自定义向下箭头按钮

发布于 2024-12-13 23:18:33 字数 755 浏览 2 评论 0原文

我们正在开发一个 C++ 形式的触摸屏 Qt 应用程序,需要宽的向下箭头图形图像以及自定义背景。我一直在尝试获得一些可以使用 QSS 工作的东西,但到目前为止都失败了。

我发现获得宽按钮(大于 16 像素)的唯一方法是使用负边距:

QComboBox {
    min-height:63px;
    max-height:63px;
    margin-right:47px;
    border-image:url(Resources/ComboBox_Center1.png);
    font-family:  "Franklin Gothic Medium";
    font-size:  22px;
}
QComboBox::drop-down {
    width:47px;
    border:0px;
    margin:0px;
    margin-right:-47px;
}
QComboBox::down-arrow {
    image:url(Resources/ComboBox_Right1.png);
}

这会将按钮放在正确的位置并使输入区域具有正确的大小,但向下箭头只会打开组合框,不打开然后关闭。

所有其他选项要么将输入区域扩展到该区域(边距、边框),要么缩小整个控件。

设置背景图像标签没有效果 - 只有边框图像显示图像。

请注意,即使在箭头下方,边框图像(或背景颜色)也始终显示。

有没有办法只设置组合框输入部分的样式?看起来组合的那部分应该有自己的子选择器,但我还没有看到它。

We're developing a touch screen Qt application in C++ forms that requires a wide down arrow graphic image as well as a custom background. I've been trying to get something that works using QSS, but have thus far been defeated.

The only way I found to get a wide button (one larger than 16 pixels) is to use negative margins:

QComboBox {
    min-height:63px;
    max-height:63px;
    margin-right:47px;
    border-image:url(Resources/ComboBox_Center1.png);
    font-family:  "Franklin Gothic Medium";
    font-size:  22px;
}
QComboBox::drop-down {
    width:47px;
    border:0px;
    margin:0px;
    margin-right:-47px;
}
QComboBox::down-arrow {
    image:url(Resources/ComboBox_Right1.png);
}

This puts the button at the correct position and makes the input area the correct size, but then the down arrow only opens the combobox, not open and then close.

All other options either extend the input area onto the area (margin, border) or shrink the entire control.

Setting the background-image tag had no effect - only the border image showed the image.

Note that the border image (or background color) always shows up even under the arrow.

Is there some way to style just the input section of a combo box? It seems like that portion of the combo should have its own child selector, but I haven't seen it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-12-20 23:18:33

试试这个...

QComboBox {
    min-height:63px;
    max-height:63px;
    font-family:  "Franklin Gothic Medium";
    font-size:  22px;
    padding: 1px 1px 1px 1px;
}
QComboBox::drop-down {
   width: 47px;
   border: 0px;
}

QComboBox::down-arrow {
    image: url(Resources/ComboBox_Right1.png);
    width: 42px;
    height: 42px;
}

注意:向下箭头图像的宽度和高度将取决于您正在使用的图像。这些对我的形象有用。

样式表有一些奇怪的事情 - 以及许多隐藏的技巧。可能值得花几分钟阅读,其中描述了样式表工作原理背后的基本原则。它们看起来很简单,但即使使用了一段时间后,我仍然经常回顾示例和文档。

背景图像是那些不明显的东西之一。这是我直接从文档中获取的内容。在大多数情况下,当设置背景图像时,我们真正想要的是设置边框图像。我假设您看不到“背景图像”的原因是组合框是一个“复杂”小部件 - 这意味着它由其他几个小部件组成 - 因此背景隐藏在它们后面。

尝试background-image属性是很常见的,但这有一个
缺点:比如背景会经常出现
隐藏在按钮装饰后面,因为它不被视为
背景。此外,如果调整按钮的大小,整个
背景将被拉伸或平铺,这并不总是看起来
好的。最好使用 border-image 属性,因为它总是
显示图像,无论背景如何(您可以将其组合
如果它有 alpha 值,则带有背景),并且它有特殊的
设置来处理按钮大小调整。

至于输入区域的样式...假设您的 QComboBox 是可编辑的,则输入区域只是一个 QLineEdit。因此,您始终可以通过这种方式将样式表分配给行编辑,

comboBox->lineEdit()->setStyleSheet(your style sheet);

我希望有所帮助。

Try this...

QComboBox {
    min-height:63px;
    max-height:63px;
    font-family:  "Franklin Gothic Medium";
    font-size:  22px;
    padding: 1px 1px 1px 1px;
}
QComboBox::drop-down {
   width: 47px;
   border: 0px;
}

QComboBox::down-arrow {
    image: url(Resources/ComboBox_Right1.png);
    width: 42px;
    height: 42px;
}

Note: the width and height on the down-arrow image will depend on the image you are using. These work for my image.

There are some weird things about style-sheets - and a lot of hidden tricks. It might be worthwhile taking a few minutes to go through this which describes the basic principles behind how style sheets work. They seem easy, but even after using them for a while, I often go back to the examples and documentation.

Background images are one of those things that is not obvious. Here is something I took directly from the doc. In most cases when setting an image for the background what we really want is to set the border-image. I'm assuming the reason you don't see the "background-image" is that the combo-box is a "complex" widget - meaning that it is made up of a few others - and so that background is hidden behind them.

It is common to try the background-image property, but this has a
number of drawbacks: For instance, the background will often appear
hidden behind the button decoration, because it is not considered a
background. In addition, if the button is resized, the entire
background will be stretched or tiled, which does not always look
good. It is better to use the border-image property, as it will always
display the image, regardless of the background (you can combine it
with a background if it has alpha values in it), and it has special
settings to deal with button resizing.

As for styling the input area... assuming your QComboBox is editable, the input area is just a QLineEdit. So you could always assign a style sheet to the line edit this way

comboBox->lineEdit()->setStyleSheet(your style sheet);

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文