Pyqt coloring part of text in QlistWidget
Asked Answered
B

1

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.

Biogen answered 12/12, 2016 at 18:28 Comment(0)
T
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

Transcaucasia answered 12/12, 2016 at 20:19 Comment(5)
if i want to change derction from right to left how !Biogen
Reverse the direction of text? so that it reads as in my example "1 tsettset"?Transcaucasia
Have you tried the answer or suggestions in the comments for #26379400 ?Transcaucasia
not label a widget look at image here hpics.li/b08bef4 i want from right to leftBiogen
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.Transcaucasia

© 2022 - 2024 — McMap. All rights reserved.