在 Qt 中使用 CSS 自定义向下箭头按钮
我们正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个...
注意:向下箭头图像的宽度和高度将取决于您正在使用的图像。这些对我的形象有用。
样式表有一些奇怪的事情 - 以及许多隐藏的技巧。可能值得花几分钟阅读此,其中描述了样式表工作原理背后的基本原则。它们看起来很简单,但即使使用了一段时间后,我仍然经常回顾示例和文档。
背景图像是那些不明显的东西之一。这是我直接从文档中获取的内容。在大多数情况下,当设置背景图像时,我们真正想要的是设置边框图像。我假设您看不到“背景图像”的原因是组合框是一个“复杂”小部件 - 这意味着它由其他几个小部件组成 - 因此背景隐藏在它们后面。
至于输入区域的样式...假设您的 QComboBox 是可编辑的,则输入区域只是一个 QLineEdit。因此,您始终可以通过这种方式将样式表分配给行编辑,
我希望有所帮助。
Try this...
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.
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
I hope that helps.