为什么聚焦时 QLineEdit 样式不改变?
我正在使用 Qt 及其样式表开发 GUI。在主窗口样式表上,我放置了以下样式:
QLineEdit:focus {
border: 2px solid #006080;
}
但是当我使用它时,样式并没有像我预期的那样真正改变。但是,如果我将相同的样式表直接放在所需的组件上,它就会像魔术一样工作!但是,将样式表放在我可能想要的每个 LineEdit 上并不是一个好主意(这会大大增加添加新组件或更改样式表所需的工作量),也不是通过添加诸如 < 之类的代码行来重新应用样式表代码>setStyleSheet(styleSheet())。
有谁知道如何解决这个问题?
I'm developing a GUI using Qt and its stylesheets. On the main window stylesheet I've put the following style:
QLineEdit:focus {
border: 2px solid #006080;
}
But when I use it the style doesn't really change as I expected. However, if I put the same stylesheet directly on the desired component, it works like magic! But well, it's not really a good idea to put stylesheets on every single LineEdit I may want (which would greatly increase the amount of work needed to add new components or change the stylesheet), neither reapplying the stylesheet by adding code lines such as setStyleSheet(styleSheet())
.
Does anyone know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果需要,您可以通过编程方式设置焦点样式,如下所示:
You can set focus styles programmatically if needed like this:
奇怪的是,它在我的 Qt 副本上使用 QLineEdit:focus 使用
你确定你没有子样式在更远的地方推翻这个吗?由于它位于主窗口上,因此它将是第一个被否决的事情。
一个潜在的解决方法是使用事件过滤器:
当然,QStyleSheets 只是 QString,因此您可以存储预定义的样式以供使用。
Odd, it works as desired on my copy of Qt using QLineEdit:focus using
Are you sure you do not have a child style somewhere farther down the line overruling this? As it is on the MainWindow it will be the first thing to be overruled.
A potential work-around is to use an event filter:
Of course QStyleSheets are simply QStrings so you can have pre-defined styles stored away for use.