PyQt5: "No Fill" in Color Dialog?

1.1k views Asked by At

I am coding a GUI in PyQt5, where I need a color picker. So far, I use the QColorDialog Class, which works fine for selecting a color - but my problem is that there seems to be no way to select "no color" (or "no fill", like its known from PowerPoint or Adobe Illustrator).

How to achieve to select "no color"? (The documentation only mentions a flag for transparency, but this is not helpful for me...)

1

There are 1 answers

0
ekhumoro On BEST ANSWER

If you don't mind using a non-native dialog, it is quite easy to customize it.

Below is a very basic implementation that shows how to embed the existing dialog, and add an extra "No Color" button at the bottom. The rest of the implementation is left as an exercise for the reader...

from PyQt5 import QtCore, QtWidgets

class ColorDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        widget = QtWidgets.QColorDialog()
        widget.setWindowFlags(QtCore.Qt.Widget)
        widget.setOptions(
            QtWidgets.QColorDialog.DontUseNativeDialog |
            QtWidgets.QColorDialog.NoButtons)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(widget)
        hbox = QtWidgets.QHBoxLayout()
        hbox.addWidget(QtWidgets.QPushButton('No Color'))
        hbox.addWidget(QtWidgets.QPushButton('Cancel'))
        hbox.addWidget(QtWidgets.QPushButton('Ok'))
        layout.addLayout(hbox)

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    dialog = ColorDialog()
    dialog.show()
    sys.exit(app.exec_())