#Get backgroundColor from QListWidgetItem
#This is the example code for Get backgroundColor from QListWidgetItem
#If your are not expecting this answer, sorry.
#Thanks, Subin Gopi
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.listWidget = QtGui.QListWidget(self)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 200, 150))
self.listWidget.setObjectName ('listWidget')
self.resize(250, 200)
self.listWidget.itemPressed.connect (self.getColor)
self.addItems()
def addItems (self) :
itemList = {'Apple':'red', 'Orange':'#ff5500', 'Grape':'blue', 'Bean':'green', 'Banana':'yellow'}
for eachItem in itemList :
currentItem = QtGui.QListWidgetItem (self.listWidget)
#Set Color
bgBrush = QtGui.QBrush()
bgBrush.setStyle(QtCore.Qt.SolidPattern)
#Qt.GlobalColor
bgBrush.setColor (QtGui.QColor(itemList[eachItem]))
#RGB Value
#bgBrush.setColor (QtGui.QColor(170, 85, 255))
currentItem.setBackground(bgBrush)
fgBrush = QtGui.QBrush()
fgBrush.setColor (QtGui.QColor('black'))
currentItem.setForeground (fgBrush)
#Set text
currentItem.setText(eachItem)
def getColor (self) :
currentItem = self.listWidget.selectedItems()
currentBrush = currentItem[-1].background()
rgbColor = currentBrush.color().red(), currentBrush.color().green(), currentBrush.color().blue()
strColor = currentBrush.color().name()
print ('rgb- Color\t', rgbColor)
print ('str- Color\t', strColor, '\n')
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
app.exec_()
was ist das Ergebnis von Druck (dir (Artikel)) sehen? –
Frage aktualisiert –