Created
September 20, 2016 10:23
-
-
Save justinfx/5926db12d275737e5eb66c7bf7e77e93 to your computer and use it in GitHub Desktop.
Qt Example: Double clicking a QListWidgetItem and toggling the background color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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