def editColor(self): button_clicked = self.sender() index = self.btnClass.index(button_clicked) label_name = self.lineeditClass[index].text() color_dlg = QColorDialog(self) color = self.labels[label_name] r = color[0] g = color[1] b = color[2] current_color = QColor(r, g, b, 255) color_dlg.setCustomColor(0, current_color) selected_color = color_dlg.getColor() r = selected_color.red() g = selected_color.green() b = selected_color.blue() self.labels[label_name] = [r, g, b] style_text = "QPushButton:flat {background-color: rgb(" + str( r) + "," + str(g) + "," + str(b) + "); border: none ;}" button_clicked.setStyleSheet(style_text)
def _show_color_dialog(current_color, parent): dialog = QColorDialog() for idx, color in enumerate(DEFAULT_COLORS): dialog.setCustomColor(idx, color) current_color = dialog.getColor(current_color, parent, 'Select line color') if current_color.isValid(): return current_color
def on_user_event(self): colord = QColorDialog()#self._color) colord.setOption(QColorDialog.DontUseNativeDialog) if self._kind == "rgbf": colord.setOption(QColorDialog.ShowAlphaChannel) colord.setCurrentColor(self._color) colord.setCustomColor(0, self._color) if colord.exec(): self.update_color(colord.currentColor())
def task_color_dialog(self): task_color = QColor(self.current_task_color) dialog = QColorDialog() if task_color is None: task_color = Qt.white dialog.setCustomColor(0, task_color) color = dialog.getColor() if color.isValid(): self.current_task_color = color.name() self.set_bg_color_to_widget(self.current_task_color, self.widgetColorTask)
def AA(self): s = self.cd.customCount() # 返回自定义颜色的格数 s = QColorDialog.customCount() # 返回自定义颜色的格数 # 16 QColorDialog.setCustomColor(0, QColor(255, 0, 0)) # 在自定义面板中自定义颜色 # 参数1 格的序号 # 参数2 颜色 # 在显示之前设置好 s = QColorDialog.customColor(0) # 获取自定义面板的颜色 -> QColor # 参数 格序号 QColorDialog.setStandardColor(5, QColor(0, 0, 255)) # 在标准颜色面板中自定义颜色 # 参数1 格的序号 # 参数2 颜色 s = QColorDialog.standardColor(5) # 获取标准颜色面板的颜色 -> QColor # 参数 格序号 self.cd.show()
class ColorPicker(QToolButton): colorReset = pyqtSignal() def __init__(self, action: QAction = None, color_group=None, default_color=Qt.blue, *args, **kwargs): super().__init__(*args, **kwargs) self.last_color_action = action if action is not None else QAction( self) self._color_group = color_group dialog_action = QWidgetAction(self) self._dialog = QColorDialog(self) self.btReset = None button_box = self._dialog.findChild(QDialogButtonBox, "") if button_box is not None: self.btReset = button_box.addButton(QDialogButtonBox.Reset) self._dialog.setWindowFlags(Qt.Widget) self._dialog.setOptions(self._dialog.options() | QColorDialog.DontUseNativeDialog) dialog_action.setDefaultWidget(self._dialog) # Hide pick screen color button button = self._dialog.findChild(QAbstractButton) if button: button.hide() # Load custom colors settings = QSettings() settings.beginGroup("Colors") custom_colors = settings.value("Custom", []) for i, color in enumerate(custom_colors): self._dialog.setCustomColor(i, color) current_color = QColor(default_color) if color_group is None\ else settings.value(f"{color_group}/Current", QColor(default_color), type=QColor) if current_color.isValid(): self._dialog.setCurrentColor(current_color) self.on_color_selected(current_color) settings.endGroup() menu = QMenu() menu.addAction(dialog_action) self.setMenu(menu) self.setPopupMode(QToolButton.MenuButtonPopup) self.setDefaultAction(self.last_color_action) # Connect events self.colorSelected = self._dialog.colorSelected if self.btReset is not None: self.btReset.clicked.connect(self.colorReset.emit) self.currentColorChanged = self._dialog.currentColorChanged menu.aboutToShow.connect(self._dialog.show) self._dialog.rejected.connect(menu.hide) self._dialog.colorSelected.connect(menu.hide) self._dialog.colorSelected.connect(self.on_color_selected) self.last_color_action.triggered.connect(self.on_use_last_color) def setIcon(self, icon: QIcon): super().setIcon(icon) self.update_icon() def showEvent(self, *args, **kwargs): self.update_icon() super().showEvent(*args, **kwargs) # noinspection PyUnusedLocal def on_color_selected(self, color): self.update_icon() self.save_settings() def save_settings(self): settings = QSettings() settings.beginGroup("Colors") custom_colors = [ self._dialog.customColor(i) for i in range(self._dialog.customCount()) ] settings.setValue('Custom', custom_colors) if self._color_group is not None: settings.setValue(f"{self._color_group}/Current", self._dialog.currentColor()) def update_icon(self): """Show selected color on top-left edge of icon""" current_icon = self.icon() icon = QIcon() for size in current_icon.availableSizes(): pixmap = current_icon.pixmap(size) painter = QPainter(pixmap) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(self._dialog.currentColor())) painter.drawEllipse(0, 0, int(size.width() / 4), int(size.height() / 4)) painter.end() icon.addPixmap(pixmap) super().setIcon(icon) def on_use_last_color(self): self.colorSelected.emit(self._dialog.currentColor())