Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Qt Example: Double clicking a QListWidgetItem and toggling the background color
"""
https://groups.google.com/d/topic/python_inside_maya/eZOCjK2jewY/discussion
How to create a QListWidget, and toggle the background color of items
when they are double clicked
"""
from PySide import QtCore, QtGui
class List(QtGui.QDialog):
def __init__(self):
super(List, self).__init__()
self._listWidget = QtGui.QListWidget()
self._listWidget.addItems(['a', 'b', 'c'])
self._red = QtGui.QBrush(QtCore.Qt.red)
self._green = QtGui.QBrush(QtCore.Qt.green)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self._listWidget)
self._listWidget.itemDoubleClicked.connect(self._handleDoubleClick)
def _handleDoubleClick(self, item):
color = item.background()
item.setBackground(self._red if color == self._green else self._green)
item.setSelected(False)
if __name__ == '__main__':
app = QtGui.QApplication([])
d = List()
d.show()
d.raise_()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment