I am running into strange behavior where setAlpha is not working on a QColor object. I do not understand why it is not working, but I am sure there is a good reason that I am just not aware of yet.
Here is the issue in a small scale example:
from PyQt4 import QtGui
clr = QtGui.QColor('yellow')
qtwi = QtGui.QTableWidgetItem()
qtwi.setBackgroundColor(clr)
print 'Colors are the same object: %s' % (clr == qtwi.backgroundColor())
print 'Alpha before is: %s' % clr.alpha()
clr.setAlpha(67)
print 'Alpha after is: %s' % clr.alpha()
print 'Alpha before is: %s' % qtwi.backgroundColor().alpha()
qtwi.backgroundColor().setAlpha(171)
print 'Alpha after is: %s' % qtwi.backgroundColor().alpha()
print 'Colors are the same object: %s' % (clr == qtwi.backgroundColor())
The results should read:
Colors are the same object: True
Alpha before is: 255
Alpha after is: 67
Alpha before is: 255
Alpha after is: 255
Colors are the same object: False
This makes no sense to me. I am clearly setting the alpha value to 171 in the second part of the example, why is it not working? Furthermore, if clr and qtwi.backgroundColor() are the same object at the beginning, why are they not longer the same at the end? What is going on here? I am quite confused. Thanks.
qtwi.backgroundColor()is returning unique instances of the same color. If you change one of the instances, it will not change the original color.If you run:
You will get:
They are two different objects. They are copies of the original. If you change the copy, it will not change the original.
However, if you set
qtwi.backgroundColor()as a variable, your code will work. If you try:You should get the results you are looking for.