答案 0 :(得分:3)
您是否尝试将QListWidgetItem文本的一部分或整个文本的颜色设置为单个QListWidgetItem?
如果要为整个单个QListWidgetItem着色,请使用item.setTextColor()。此示例将第二个项目的文本着色为红色:
from PyQt4 import QtGui, QtCore
import sys
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self,parent)
self.initUI()
def initUI(self):
mylist = QtGui.QListWidget(self)
mylist.setMinimumSize(QtCore.QSize(800, 800))
for i in range(5):
item = mylist.addItem('Item %s' % (i + 1))
items = mylist.findItems("Item 2",QtCore.Qt.MatchExactly)
if len(items) > 0:
for item in items:
item.setTextColor (QtGui.QColor("red"))
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
这给出了这个:
或者只是QListWidgetItem中的部分文本使用带有addWidget()和HTML的QLabel:
from PyQt4 import QtGui, QtCore
import sys
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self,parent)
self.initUI()
def initUI(self):
mylist = QtGui.QListWidget(self)
mylist.setMinimumSize(QtCore.QSize(800, 800))
for i in range(5):
widgitItem = QtGui.QListWidgetItem()
widget = QtGui.QWidget()
widgetText = QtGui.QLabel('test<span style="color:#ff0000;">test %s</span>' % (i + 1))
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
widget.setLayout(widgetLayout)
mylist.addItem(widgitItem)
widgitItem.setSizeHint(widget.sizeHint())
mylist.setItemWidget(widgitItem, widget)
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
这给出了这个