How to change the text color of an item in a QListWidget?

4,856 views
Skip to first unread message

Juan Tigreros

unread,
Sep 20, 2016, 4:54:50 AM9/20/16
to Python Programming for Autodesk Maya
Hi everyone! 

Perhaps this is a simple question but I can seemed to succeed at googling it. Maybe someone here can help?

I would like to change the text color of a text or background color if the item in a qlistwidget? I am double clicking on an item and will like to change the color of it, then change it back once i double clicked in another item in the list. Any ideas?

Justin Israel

unread,
Sep 20, 2016, 5:05:13 AM9/20/16
to Python Programming for Autodesk Maya
Check out setForeground and setBackground on the QListWidgetItem 



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3f1cd8c0-0f1f-471a-80e8-49205b0dbe36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Tigreros

unread,
Sep 20, 2016, 5:48:15 AM9/20/16
to Python Programming for Autodesk Maya
Thanks for the quick response Justin.

First, I should mention that I am fairly new to this. With that said, here is what im trying to do. 
    def doubleClickedItem(self):
        """for when an item is double clicked """
 
        theListWidget = self.sender()
        currentItem = theListWidget.currentItem()
        currentItemText = currentItem.text()
        currentItem.setBackground(QtGui.QColor('red'))


So, the idea is that I capture the sender item and changed its color to red after is doublecliked. But all the documentation I can find is in c++ and have not come across python examples. I am sure its prob very easy to port those examples to python, however, i am still learning and currently stumped by this. 

Alok Gandhi

unread,
Sep 20, 2016, 6:12:56 AM9/20/16
to python_in...@googlegroups.com
Take a loot at the pyside docs here:
https://srinikom.github.io/pyside-docs/contents.html

You can also use `PySide.QtGui.QWidget.setStyleSheet(styleSheet)` to update the colors.


For capturing double click, another approach would be apply an even filter:
def clickable(widget):
    """Re implementation Double Click Event on a widget"""
    class MouseDoubleClickFilter(QtCore.QObject):
        doubleClicked = QtCore.pyqtSignal()

        def eventFilter(self, obj, event):
            if obj == widget:
                if event.type() == QtCore.QEvent.MouseButtonDblClick:
                    self.doubleClicked.emit()
                    return True

            return False

    filter = MouseDoubleClickFilter(widget)
    widget.installEventFilter(filter)

    return filter.doubleClicked


And then connect this signal:
# Connect the double click signal
clickable(qlistwidget).connect(handleDoubleClick)

def handleDoubleClick():
    # Implement your color update logic here
    pass


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/78f19fba-ce1b-4697-b768-e9d686c78b40%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Justin Israel

unread,
Sep 20, 2016, 6:25:01 AM9/20/16
to python_in...@googlegroups.com
I've thrown together an example here:

QListWidget lets you connect to a double click signal which hands you the item that was double clicked.
From there it is trivial to toggle the color as needed.

Justin

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.



--

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMSbTKJqWpRjf24%3DHCsatoBwXhfe-2guE1eDgH5%2BNmuQFQ%40mail.gmail.com.

Juan Tigreros

unread,
Sep 20, 2016, 6:36:05 AM9/20/16
to Python Programming for Autodesk Maya
Thanks Justin!!! This was the missing piece. :) I truly appreciate the help. 


On Tuesday, September 20, 2016 at 10:54:50 AM UTC+2, Juan Tigreros wrote:
Reply all
Reply to author
Forward
0 new messages