I would like to color part of text in QListWidget
I tried to include tag font int text but this does not work.
I would like to color part of text in QListWidget
I tried to include tag font int text but this does not work.
Are you trying to colour part of a QListWidgetItem text or the whole of the text a single QListWidgetItem?
If you want to colour the whole of a single QListWidgetItem use item.setTextColor(). This example colours the text of the second item red:
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()
Which gives this:
Or just part of the text in a QListWidgetItem use QLabel with addWidget() and HTML:
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()
Which gives this
© 2022 - 2024 — McMap. All rights reserved.