6

I would like to color part of text in QListWidget

enter image description here

I tried to include tag font int text but this does not work.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
CodingX
  • 115
  • 2
  • 8

1 Answers1

10

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:

enter image description here

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

Gives

Dan-Dev
  • 8,957
  • 3
  • 38
  • 55
  • if i want to change derction from right to left how ! – CodingX Dec 13 '16 at 13:36
  • Reverse the direction of text? so that it reads as in my example "1 tsettset"? – Dan-Dev Dec 13 '16 at 13:57
  • Have you tried the answer or suggestions in the comments for http://stackoverflow.com/questions/26378900/set-text-direction-for-qlabel ? – Dan-Dev Dec 13 '16 at 14:05
  • not label a widget look at image here http://hpics.li/b08bef4 i want from right to left – CodingX Dec 13 '16 at 14:26
  • I think this deserves a stearate question. of it's own. Could you ask a new question be very clear as to what it is you have currently and what it is you want. I'll try to take a look later. Meanwhile if this answer solves the question you originally asked feel free to accept it and/or vote for it using the buttons to the left of the answer. – Dan-Dev Dec 13 '16 at 14:46