我在更改 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)
在这里,我设法将实际框的突出显示颜色更改为灰色:
但这里下拉突出显示仍然是蓝色:
感谢所有帮助。
最佳答案
根据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/