简体   繁体   中英

PyQt5: “No Fill” in Color Dialog?

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...)

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM