2012-10-09 82 views
4

在Qt4/PyQt4中对QIcon应用色调的最简单方法是什么?我有几个单色QPixmaps,我想根据其关联对象的属性重新着色。Qt4:对QIcon进行着色

回答

6

你可以画在你的像素图。只需选择适合您的目标的composition mode即可。

下面是一个简单的Tinter工具。 applyTint方法是有趣的部分。这使用Overlay组成。

import sys 
from PyQt4 import QtGui, QtCore 

class Tinter(QtGui.QWidget): 
    def __init__(self, image, parent=None): 
     super(Tinter, self).__init__(parent) 

     self.pixmap = QtGui.QPixmap(image) 

     self.normal = QtGui.QLabel() 
     self.normal.setPixmap(self.pixmap) 
     self.tinted = QtGui.QLabel() 

     self.red = QtGui.QSlider(QtCore.Qt.Horizontal) 
     self.red.setRange(0, 255) 
     self.red.sliderMoved.connect(self.applyTint) 
     self.green = QtGui.QSlider(QtCore.Qt.Horizontal) 
     self.green.setRange(0, 255) 
     self.green.sliderMoved.connect(self.applyTint) 
     self.blue = QtGui.QSlider(QtCore.Qt.Horizontal) 
     self.blue.setRange(0, 255) 
     self.blue.sliderMoved.connect(self.applyTint) 
     self.alpha = QtGui.QSlider(QtCore.Qt.Horizontal) 
     self.alpha.setRange(0, 255) 
     self.alpha.setValue(128) 
     self.alpha.sliderMoved.connect(self.applyTint) 


     controlLayout = QtGui.QFormLayout() 
     controlLayout.addRow('red', self.red) 
     controlLayout.addRow('green', self.green) 
     controlLayout.addRow('blue', self.blue) 
     controlLayout.addRow('alpha', self.alpha) 

     layout = QtGui.QHBoxLayout() 
     layout.addWidget(self.normal) 
     layout.addWidget(self.tinted) 
     layout.addLayout(controlLayout) 
     self.setLayout(layout) 

     self.applyTint() 

    def applyTint(self): 
     temp = QtGui.QPixmap(self.pixmap) 
     color = QtGui.QColor(self.red.value(), 
          self.green.value(), 
          self.blue.value(), 
          self.alpha.value()) 
     painter = QtGui.QPainter(temp) 
     painter.setCompositionMode(painter.CompositionMode_Overlay) 
     painter.fillRect(temp.rect(), color) 
     painter.end() 
     self.tinted.setPixmap(temp) 

app = QtGui.QApplication(sys.argv) 

main = Tinter('so.jpg') 
main.show() 

sys.exit(app.exec_()) 

enter image description here

+0

这很完美。谢谢 – ChrisB

1

如果图标可以显示在其自己的小部件中(例如QLabel),那么一个简单的解决方案是应用QGraphicsColorizeEffect

这里有一个简单的演示:

from random import randint 
from PyQt4 import QtGui, QtCore 

class Window(QtGui.QWidget): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     self.label = QtGui.QLabel(self) 
     self.label.setPixmap(QtGui.QPixmap('image.jpg')) 
     self.button = QtGui.QPushButton('Tint', self) 
     self.button.clicked.connect(self.handleButton) 
     layout = QtGui.QVBoxLayout(self) 
     layout.addWidget(self.label) 
     layout.addWidget(self.button) 

    def handleButton(self): 
     if self.label.graphicsEffect() is None: 
      self.effect = QtGui.QGraphicsColorizeEffect(self) 
      self.effect.setStrength(0.6) 
      self.label.setGraphicsEffect(self.effect) 
     self.effect.setColor(QtGui.QColor(
      randint(0, 255), randint(0, 255), randint(0, 255))) 

if __name__ == '__main__': 

    import sys 
    app = QtGui.QApplication(sys.argv) 
    window = Window() 
    window.show() 
    sys.exit(app.exec_()) 
+0

谢谢,好像正是我要找的。不幸的是,它不能在我的Mac上运行(它在Linux上)。这似乎是一个已知但被忽略的错误 - https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCAQFjAA&url=https%3A%2F%2Fbugreports.qt -project.org%2Fbrowse%2FQTBUG-7550%3Fpage%3Dcom.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel&ei = ln51UPXjIImo0AHNqYGoCg&usg = AFQjCNHy0gKn_bpDHO43wbXtK-sFQ8QMEQ – ChrisB

相关问题