Get the advanced color picker only, from QColorDialog
-
Is it possible to load advanced picker from QColorDialog into a widget? By advanced picker I mean like the one in the picture.
I made a color picker with colors of my choice but still want to let the user define their own colors and add it to a custom colors list. I could write something like the one in the picture myself but was thinking if it possible to just get the thing from QColorDialog and add it into a layout/widget. -
Hi
Well the
class QColorPicker : public QFrame
{is only defined in .cpp it seems its not possible to reuse.
But we can steal one from a QColroDialog and insert into a layout
But im not use if we can connect to it and use it really.
btw i steal it like this
QColorDialog d; d.dumpObjectTree(); // to see the class names inside.. auto list = d.findChildren<QWidget *>(); for (QWidget *w : list) { if (strcmp( w->metaObject()->className(), "QColorPicker") == 0) ui->test->addWidget(w); // test is a layout }
-
@mrjj I tried implementing this in PySide2. This method was just not going well with my full code for some reason. Ended up writing a whole new class and made my own advanced color picker (not complete) it gives me more control too. But this is a nice method and can actually be used in certain cases. Thank you for your effort.
-
@tIk90wT
Hi
Well since we can't use the type QColorPicker, it was not really possible to connect to its signals
so it was not very useful anyway.
So a custom color picker sounds much better.
If it turns out good, maybe share it with the world ? -
Hi there!
It seems that this topic is quite old, but when I've tried to make something similar - it came as a first result in google and seemed to be quite useful.
That is why, I've decided to add, that there is a signal, which is emitted by QColorPicker, which can be pulled and processed.
According to Qt4 source code (don't think it was heavily changed), signal newCol(hue, saturation) is emitted when color is picked.
So, you can reconstruct color with QColor().setHSV(hue, saturation, 200 (as in source) )Here is my PyQt5 code for demonstration:
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys class ColorLabel(QLabel): color_changed = pyqtSignal(QColor) def __init__(self, color: QColor = None, *args, **kwargs): super(ColorLabel, self).__init__(*args, **kwargs) self.color = color self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.setMinimumHeight(50) if not self.color: self.color = QColor("white") @pyqtSlot(int, int, int) def set_color_from_hsv(self, hue: int, saturation: int, value: int): self.color = QColor() self.color.setHsv(hue, saturation, value) self.color_changed.emit(self.color) self.update() def paintEvent(self, e: QPaintEvent): painter = QPainter(self) painter.fillRect(self.rect(), self.color) painter.end() class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) cd = QColorDialog() colorPicker = [c for c in cd.children() if c.metaObject().className() == "QColorPicker"][0] colorPicker.newCol.connect(self.newCol) self.color_show = ColorLabel(QColor("green"), self) # Also, you can use color Picker with QColorLuminancePicker to set hue, saturation and value for color # colorLuminancePicker = [c for c in cd.children() if c.metaObject().className() == "QColorLuminancePicker"][0] # cp_layout.addWidget(colorLuminancePicker) # colorLuminancePicker.newHsv.connect(color_show.set_color_from_hsv) layout = QVBoxLayout() mainWidget = QWidget(self) mainWidget.setLayout(layout) cp_layout = QHBoxLayout() layout.addLayout(cp_layout) cp_layout.addWidget(colorPicker) layout.addWidget(self.color_show) self.setCentralWidget(mainWidget) def newCol(self, hue:int, saturation:int, value:int=200): self.color_show.set_color_from_hsv(hue, saturation, value) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec_()
If you want to set "value" for HSV dinanically too, you may take not only QColorPicker, but also QColorLuminancePicker. It emits newHsv(hue, saturation, value) signal.