def initUI(self): # Create some widgets (these won't appear immediately): self.leftWidget = QWidget() self.pointsCombobox = QComboBox() self.pointsCombobox.setFocusPolicy(Qt.NoFocus) self.pointsCombobox.currentIndexChanged.connect(self.datasetChanged) self.leftWidget.setLayout(QFormLayout()) self.leftWidget.layout().addRow("Point sets", self.pointsCombobox) self.addSlider(self.leftWidget.layout(), text="Point Size", max=15, callback=self.update_point_size) cDialog = QColorDialog(self) cDialog.currentColorChanged.connect(self.update_color) cDialog.setWindowFlags(Qt.Widget) cDialog.setOptions(QColorDialog.DontUseNativeDialog | QColorDialog.NoButtons) cDialog.setFocusPolicy(Qt.NoFocus) self.leftWidget.layout().addRow(cDialog) self.leftWidget.setFixedWidth(600) self.mywidget = Window(data_filepath=None) # Put the widgets in a layout (now they start to appear): layout = QGridLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.mywidget, 0, 1) layout.addWidget(self.leftWidget, 0, 0) self.widget.setLayout(layout)
def __selectColourSlot(self, button): """ Private slot to select a color. @param button reference to the button been pressed @type QPushButton """ colorKey = button.property("colorKey") hasAlpha = button.property("hasAlpha") colDlg = QColorDialog(self) if hasAlpha: colDlg.setOptions(QColorDialog.ShowAlphaChannel) # Set current colour last to avoid conflicts with alpha channel colDlg.setCurrentColor(self.__coloursDict[colorKey][0]) colDlg.currentColorChanged.connect( lambda col: self.colourChanged.emit(colorKey, col)) colDlg.exec_() if colDlg.result() == colDlg.Accepted: colour = colDlg.selectedColor() size = button.iconSize() pm = QPixmap(size.width(), size.height()) pm.fill(colour) button.setIcon(QIcon(pm)) self.__coloursDict[colorKey][0] = colour # Update colour selection self.colourChanged.emit(colorKey, self.__coloursDict[colorKey][0])
def mouseDoubleClickEvent(self, event): if self._readOnly: return dialog = QColorDialog(self._color) dialog.setOptions(QColorDialog.ShowAlphaChannel) ok = dialog.exec_() if ok: self.setColor(dialog.currentColor()) self.colorChanged.emit()
def pickColor(self): if self._readOnly: return dialog = QColorDialog(self._color) dialog.setOptions(QColorDialog.ShowAlphaChannel) ok = dialog.exec_() if ok: self.setColor(dialog.currentColor()) self.colorChanged.emit()
def cp(): cd = QColorDialog(parent=vis.get_main_window()) cd.setWindowFlag(Qt.WindowStaysOnTopHint, True) cd.setOptions(QColorDialog.ShowAlphaChannel) cd.setCurrentColor(QColor.fromRgbF(*vis.get_added_matter_color())) cd.exec() if cd.result() == 1: vis.set_added_matter_color((cd.selectedColor().getRgbF()))
def getColorPolygonAndBrushStyle( initial_color: Union[QColor, Qt.GlobalColor, QGradient] = QColor(), initial_polygon: NodePolygon = NodePolygon.Circle, initial_brushstyle: Qt.BrushStyle = Qt.NoBrush, parent: Optional[QWidget] = None, title: str = '', options: Union[QColorDialog.ColorDialogOptions, QColorDialog.ColorDialogOption] = QColorDialog.ColorDialogOptions()) \ -> (QColor, NodePolygon): dlg = QColorDialog(parent) # Add buttons to select polygon polygons_group = QButtonGroup(parent) polygons_layout = QHBoxLayout(parent) for p in NodePolygon: if p == NodePolygon.Custom: continue button = QToolButton(parent) button.setCheckable(True) button.setText(p.name) button.setIcon(QIcon(nodePolygonToPixmap(p, button.iconSize()))) if p == initial_polygon: button.setChecked(True) polygons_group.addButton(button) polygons_group.setId(button, p.value) polygons_layout.addWidget(button) dlg.layout().insertLayout(dlg.layout().count()-1, polygons_layout) # Add buttons to select brush style brushstyle_group = QButtonGroup(parent) brushstyle_layout = QHBoxLayout(parent) for p in NODE_BRUSH_STYLES: button = QToolButton(parent) button.setCheckable(True) button.setIcon(QIcon(nodeBrushStyleToPixmap(p, button.iconSize()))) if p == initial_brushstyle: button.setChecked(True) brushstyle_group.addButton(button) brushstyle_group.setId(button, p) brushstyle_layout.addWidget(button) dlg.layout().insertLayout(dlg.layout().count()-1, brushstyle_layout) if title: dlg.setWindowTitle(title) dlg.setOptions(options) dlg.setCurrentColor(initial_color) dlg.exec_() return (dlg.selectedColor(), NodePolygon(polygons_group.checkedId()), brushstyle_group.checkedId(), )
def pickColor(self): """ Opens up a modal QColorDialog_ to choose a new color, and stores it in this widget if the user clicks on the Ok button. Emits *colorChanged* when successful. .. _QColorDialog: http://doc.qt.io/qt-5/qcolordialog.html """ if self._readOnly: return dialog = QColorDialog(self._color) dialog.setOptions(QColorDialog.ShowAlphaChannel) ok = dialog.exec_() if ok: self.setColor(dialog.currentColor()) self.colorChanged.emit()
class Demo(QWidget): def __init__(self): super().__init__() self.main_window = self # 初始化颜色 self.color = QColor(107, 173, 246) # 创建颜色对话框--但不显示 self.color_dialog = QColorDialog(self.color, self.main_window) # 创建调色板 self.palette = QPalette() def setup_ui(self) -> None: # 颜色选取信号 # 会向槽函数传递一个值---QColor # self.color_dialog.colorSelected.connect(self.update_color) # 选中最终颜色时发出信号-点击对话框的确定按钮时 self.color_dialog.currentColorChanged.connect(self.update_color) # 当前颜色变化时 # self.cd.setOption(QColorDialog.NoButtons) #选项控制--单个选项 # QColorDialog.NoButtons 不显示“ 确定”和“ 取消”按钮。(对“实时对话框”有用) # QColorDialog.ShowAlphaChannel 对话框中增加alpha设置项 self.color_dialog.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) # 选项控制--多个选项 # 打开对话框,获取颜色 # noinspection PyCallByClass # self.color = self.color_dialog.getColor(self.color, None) # 打开对话框 self.color_dialog.show() def update_color2(self, event: QColor): self.palette.setColor(QPalette.Background, event) # 给调色板设置颜色 self.setPalette(self.palette) # 给控件设置颜色 def update_color(self, event: QColor) -> None: # print(event, type(event)) # print(self.color_dialog.selectedColor(), type(self.color_dialog.selectedColor())) # QPalette.Background 表示设置背景色 # QPalette.WindowText 表示设置文本颜色 self.palette.setColor(QPalette.Background, event) # 给调色板设置颜色 # self.palette.setColor(QPalette.Background, self.color_dialog.selectedColor()) # 给调色板设置颜色 self.main_window.setPalette(self.palette) # 给控件设置颜色
def getColor( parent = None, title = "", color = None, alpha = False, ): """Ask the user a color.""" global _savedColor if color is None: color = _savedColor dlg = QColorDialog(color, parent) options = QColorDialog.ColorDialogOptions() if alpha: options |= QColorDialog.ShowAlphaChannel if not QSettings().value("native_dialogs/colordialog", True, bool): options |= QColorDialog.DontUseNativeDialog dlg.setOptions(options) dlg.setWindowTitle(title or app.caption(_("Select Color"))) if dlg.exec_(): _savedColor = dlg.selectedColor() return _savedColor
def getColor( parent=None, title="", color=None, alpha=False, ): """Ask the user a color.""" global _savedColor if color is None: color = _savedColor dlg = QColorDialog(color, parent) options = QColorDialog.ColorDialogOptions() if alpha: options |= QColorDialog.ShowAlphaChannel if not QSettings().value("native_dialogs/colordialog", True, bool): options |= QColorDialog.DontUseNativeDialog dlg.setOptions(options) dlg.setWindowTitle(title or app.caption(_("Select Color"))) if dlg.exec_(): _savedColor = dlg.selectedColor() return _savedColor
class Demo(QWidget): def __init__(self): super().__init__() self.resize(300, 300) self.setWindowTitle('QFontDialog') self.label = QLabel('塘沽五中', self) self.label.move(100, 10) self.cd = QColorDialog(QColor(250, 0, 0), self) # 创建颜色对话框--但不显示 # 参数1 默认颜色 self.pl = QPalette() # 创建调色板 btn = QPushButton('按钮', self) btn.move(100, 250) btn.clicked.connect(self.AA) # self.cd.setOption(QColorDialog.NoButtons) #选项控制--单个选项 # QColorDialog.NoButtons 不显示“ 确定”和“ 取消”按钮。(对“实时对话框”有用) # 参数2 on 表示有效 False表示取消 self.cd.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) # 选项控制--多个选项 # QColorDialog.ShowAlphaChannel 对话框中增加alpha设置项 self.cd.currentColorChanged.connect(self.BB) # 当前颜色变化时发出信号 def BB(self): s = self.cd.currentColor() # 返回当前颜色 # setCurrentColor(QColor()) 设置当前颜色 self.pl.setColor(QPalette.Background, s) self.setPalette(self.pl) def AA(self): r = self.cd.exec() if r: self.pl.setColor(QPalette.Background, self.cd.selectedColor()) # 给调色板设置颜色 self.setPalette(self.pl) # 给控件设置颜色
def on_pressed(self): """Button pressed event handler Shows color dialog and sets the chosen color. """ dlg = QColorDialog(self.parent()) dlg.setCurrentColor(self.color) dlg.setWindowTitle(self.title) dlg.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint) dlg.setWindowModality(Qt.ApplicationModal) dlg.setOptions(QColorDialog.DontUseNativeDialog) p = self.mapFromGlobal(QCursor.pos()) p.setX(p.x() + (self.rect().width() / 2)) p.setY(p.y() + (self.rect().height() / 2)) dlg.move(self.mapToGlobal(p)) if dlg.exec_(): self.color = dlg.currentColor() self.colorChanged.emit()
class SkinColorDialogUI(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 初始化颜色 self.color = QColor(*settings.SKIN_COLOR) # 创建颜色对话框--但不显示 # self.color_dialog = QColorDialog(self.color, self.main_window) self.color_dialog = QColorDialog(self.color, self) # 创建调色板 self.palette = QPalette() self.options() def update_color(self, event: QColor) -> None: """ 更新颜色 两种方式同时 :param event: :return: """ # print(event, type(event)) # print(self.color_dialog.selectedColor(), type(self.color_dialog.selectedColor())) # QPalette.Background 表示设置背景色 # QPalette.WindowText 表示设置文本颜色 self.palette.setColor(QPalette.Background, event) # 给调色板设置颜色 self.palette.setBrush(QPalette.Base, QBrush(QColor(*event.getRgb()))) # self.palette.setColor(QPalette.Background, self.color_dialog.selectedColor()) # 给调色板设置颜色 # 给控件设置颜色 if event.isValid(): # self.main_window.setStyleSheet('QWidget {background-color:%s}' % event.name()) # self.main_window.setPalette(self.palette) self.color_dialog.setStyleSheet('QWidget {background-color:%s}' % event.name()) self.color_dialog.setPalette(self.palette) # 发色信号 communicate.skin_color.emit(event) def options(self) -> None: """ 参数设置 """ # 颜色选取信号 # 会向槽函数传递一个值---QColor # self.color_dialog.colorSelected.connect(self.update_color) # 选中最终颜色时发出信号-点击对话框的确定按钮时 self.color_dialog.currentColorChanged.connect( self.update_color) # 当前颜色变化时 # self.cd.setOption(QColorDialog.NoButtons) #选项控制--单个选项 # QColorDialog.NoButtons 不显示“ 确定”和“ 取消”按钮。(对“实时对话框”有用) # QColorDialog.ShowAlphaChannel 对话框中增加alpha设置项 self.color_dialog.setOptions( QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) # 选项控制--多个选项 # 打开对话框,会等待完成 # noinspection PyCallByClass # self.color_dialog.getColor(self.color, None) # 最终回调函数 self.color_dialog.finished.connect(self.finished_color) def setup_ui(self) -> None: # 打开对话框,等待打开 # self.color_dialog.open() self.color_dialog.show() def finished_color(self, event: int) -> None: """ 设置颜色到全局变量 :param event: :return: """ if event: pass color = self.color_dialog.currentColor().getRgb()[:3] settings.SKIN_COLOR = color
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())