python-3.x - 如何在 PyQt 中更改 QCombobox 突出显示颜色

标签 python-3.x user-interface pyqt5 qpalette

我在更改 PyQt 中 QCombobox 的突出显示颜色时遇到问题。我已设法更改实际输入框的突出显示颜色,但当下拉列表出现时,它仍然是蓝色的。下图显示了到底发生了什么。调色板方法适用于 Linux,但不适用于 Windows(我目前正在使用的)。 我使用 PyQt 调色板:

    brush = QtGui.QBrush(QtGui.QColor(168, 168, 168))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
    self.comboBox_7.setPalette(palette)

在这里,我设法将实际框的突出显示颜色更改为灰色:

image1

但这里下拉突出显示仍然是蓝色:

image2

感谢所有帮助。

最佳答案

根据Qt docs ,调色板可能并不总是在某些平台上工作:

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the macOS styles.

Qt Style Sheets Overview建议样式表应该在调色板不起作用的地方起作用。我自己无法在 Linux 以外的任何设备上测试这一点,但以下似乎工作正常:

from PyQt5 import QtWidgets
app = QtWidgets.QApplication([''])
combo = QtWidgets.QComboBox()
combo.addItems('One Two Three'.split())
combo.setStyleSheet('selection-background-color: rgb(168,168,168)')
combo.show()
app.exec_()

关于python-3.x - 如何在 PyQt 中更改 QCombobox 突出显示颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46630148/

相关文章:

python-3.x - SQLite 数据库被锁定

c++ - QT : Cannot find moc file

python - 错误 :permission_manager_qt. cpp(82) 不支持的权限类型:13

python - 神秘的换行符被添加到 QPlainTextEdit 的末尾

python 3 : receive user input including newline characters

python - 从以特定字符开头的文件中删除空记录

python - Python 3 可以对我的 Mac 上打开的文件使用react吗?

.NET GUI 在大量数据加载时卡住

jquery - 如何获取 JQuery UI 选项卡插件中默认加载的选项卡文本?

python - 如何在 PyQt5 中更新窗口?