编程语言
首页 > 编程语言> > python – Qt4:着色QIcon

python – Qt4:着色QIcon

作者:互联网

在Qt4 / PyQt4中将颜色色调应用于QIcon的最简单方法是什么?我有几个单色QPixmaps,我想根据相关对象的属性重新着色.

解决方法:

你可以在你的像素图上画画.只需选择适合您目标的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_())

标签:python,qt4,pyqt4,tint,qicon
来源: https://codeday.me/bug/20190613/1231569.html