Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created September 20, 2016 10:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfx/5926db12d275737e5eb66c7bf7e77e93 to your computer and use it in GitHub Desktop.
Save justinfx/5926db12d275737e5eb66c7bf7e77e93 to your computer and use it in GitHub Desktop.
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