Search code examples
pythonpython-3.xpyqtpyqt5qcolordialog

Only Color Gradient from Qt5 ColorDialog


I wanted to ask if it is possible to use only the Color Gradient(red surrounded) part of the QColorDialog.

QColorDialog

I am using PyQt5 and Python3 on different Linux machines (ubuntu + raspian).


Solution

  • It is only necessary to hide all the elements except the QColorPicker and QColorLuminancePicker.

    import sys
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class ColorDialog(QtWidgets.QColorDialog):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setOptions(self.options() | QtWidgets.QColorDialog.DontUseNativeDialog)
    
            for children in self.findChildren(QtWidgets.QWidget):
                classname = children.metaObject().className()
                if classname not in ("QColorPicker", "QColorLuminancePicker"):
                    children.hide()
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
    
        colordialog = ColorDialog()
        label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
    
        def onCurrentColorChanged(color):
            label.setStyleSheet("background-color: {}".format(color.name()))
    
        colordialog.currentColorChanged.connect(onCurrentColorChanged)
        onCurrentColorChanged(colordialog.currentColor())
    
        w = QtWidgets.QWidget()
        lay = QtWidgets.QVBoxLayout(w)
        lay.addWidget(colordialog, alignment=QtCore.Qt.AlignCenter)
        lay.addWidget(label)
        w.show()
    
        sys.exit(app.exec_())
    

    enter image description here