仅Qt5 ColorDialog中的颜色渐变

问题描述 投票:0回答:1

我想问问是否可以仅使用QColorDialog的Color Gradient(红色包围)部分。

“

我正在不同的Linux机器(ubuntu + raspian)上使用PyQt5和Python3。

python python-3.x pyqt pyqt5 qcolordialog
1个回答
0
投票

仅需隐藏QColorPickerQColorLuminancePicker以外的所有元素。

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

© www.soinside.com 2019 - 2024. All rights reserved.