【问题标题】:QComboBox:focus Qt Style SheetQComboBox:focus Qt 样式表
【发布时间】:2013-02-08 18:55:01
【问题描述】:

我在输入表单上有一个可编辑组合框,当它获得焦点时,它的背景必须改变。以下代码适用于 QLineEdit,但对 QComboBox 无效。

QLineEdit, QComboBox { background-color: green; }
QLineEdit:focus, QComboBox:focus { background-color: red; }

是否有可能使 QComboBox 像 QLineEdit 一样仅使用 Qt 样式表

【问题讨论】:

  • 试试 QComboBox:!editable:focus
  • 好问题!我能够通过setStyleSheet("QLineEdit, QComboBox { background-color: green; }" "QComboBox:focus, QComboBox:focus QListView { background-color: red; }"); 到达某个地方,但似乎我错过了一些东西......
  • 即使设置这个工作,但不是背景setStyleSheet("QLineEdit, QComboBox { background-color: green; }" "QComboBox:focus, QComboBox:focus QAbstractItemView { background-color: red; selection-background-color: red}");
  • @RachelGallen 在问这里之前我已经尝试过了。
  • @NemanjaBoric:是的,我也试过了,但它只会改变所选文本的背景颜色。此外,当我使用QComboBox { background-color: transparent; } 时,我的:focus 开始工作,但我失去了失焦背景。输入padding: 2 会提供一些额外的信息——组合框前面有一些东西。它应该是QLineEdit,但它不是,或者不完全是。我试过QComboBox > QLineEdit 没有成功。

标签: qt qcombobox


【解决方案1】:

您可能需要通过继承 QLineEdit 并将其安装到组合框(使用QComboBox::setLineEdit())来完成此操作。然后,覆盖 QLineEdit 的 focusIn() 和 focusOut() 函数,并在这些函数中设置具有适当背景颜色的样式表。

另一种方法是在组合框(和/或其关联的 QLineEdit)上安装事件处理程序并捕获焦点输入/输出事件,然后更改样式表。

【讨论】:

  • 你可能是对的,但它改变了程序的代码。我的问题不太正确。我已经有了这样的解决方案,而且最简单:将QComboBox 放在QFrame 中,具有适当的背景并设置QComboBox { background-color: transparent; }。我会澄清我的问题。
猜你喜欢
  • 1970-01-01
  • 2011-11-02
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多