sigDataChanged = pyqtSignal()
def __init__(self, color=None):
if color is not None:
self._color = color
else:
self._color = "#ff0000"
self.ldtColor.setReadOnly(True)
pixmap.
fill(QColor(self.
color)) self.btnColor.setIcon(icon)
self.
btnColor.
setIconSize(QSize(24,
24)) self.btnColor.setFixedWidth(24)
self.btnColor.setFixedHeight(24)
layout.addWidget(self.ldtColor)
layout.addWidget(self.btnColor)
self.setLayout(layout)
self.setContentsMargins(0, 0, 0, 0)
self.layout().setContentsMargins(0, 0, 0, 0)
self.btnColor.clicked.connect(self.changeColor)
def changeColor(self):
print(self._color)
colorInit.setNamedColor(self._color)
color
= QColorDialog.
getColor(colorInit, self,
"Select a color") if color.isValid():
self.ldtColor.setText(color.name())
self.setColor(color.name())
def updateIcon(self):
newcolor.setNamedColor(self._color)
pixmap.fill(newcolor)
self.btnColor.setIcon(icon)
def getColor(self):
return self._color
def setColor(self, newColor):
self._color = newColor
self.ldtColor.setText(newColor)
self.updateIcon()
self.sigDataChanged.emit()
color = pyqtProperty(str, fget=getColor, fset=setColor, notify=sigDataChanged)
class ColorWidget(QWidget):
sigDataChanged = pyqtSignal()
def __init__(self, color=None):
QWidget.__init__(self)
if color is not None:
self._color = color
else:
self._color = "#ff0000"
self.ldtColor = QLineEdit(self.color)
self.ldtColor.setReadOnly(True)
self.btnColor = QPushButton()
pixmap = QPixmap(24, 24)
pixmap.fill(QColor(self.color))
icon = QIcon(pixmap)
self.btnColor.setIcon(icon)
self.btnColor.setIconSize(QSize(24, 24))
self.btnColor.setFixedWidth(24)
self.btnColor.setFixedHeight(24)
layout = QHBoxLayout()
layout.addWidget(self.ldtColor)
layout.addWidget(self.btnColor)
self.setLayout(layout)
self.setContentsMargins(0, 0, 0, 0)
self.layout().setContentsMargins(0, 0, 0, 0)
self.btnColor.clicked.connect(self.changeColor)
def changeColor(self):
colorInit = QColor()
print(self._color)
colorInit.setNamedColor(self._color)
color = QColorDialog.getColor(colorInit, self, "Select a color")
if color.isValid():
self.ldtColor.setText(color.name())
self.setColor(color.name())
def updateIcon(self):
pixmap = QPixmap(24, 24)
newcolor = QColor()
newcolor.setNamedColor(self._color)
pixmap.fill(newcolor)
icon = QIcon(pixmap)
self.btnColor.setIcon(icon)
def getColor(self):
return self._color
def setColor(self, newColor):
self._color = newColor
self.ldtColor.setText(newColor)
self.updateIcon()
self.sigDataChanged.emit()
color = pyqtProperty(str, fget=getColor, fset=setColor, notify=sigDataChanged)
To copy to clipboard, switch view to plain text mode
Bookmarks