Search code examples
pythonpysideqt-signalsqcolorqcolordialog

Pyside - QcolorDialog signals


What would be the signal for clicking the 'ok' button in the QColorDialog.

I tried

self.color_chooser = QtWidgets.QColorDialog()
self.color_chooser.getColor()
self.color_chooser.currentColorChanged.connect(self.color_pick)

def color_pick(self):
    print 'signaled'

that did not work.


Solution

  • The signal you are requesting is colorSelected, this is issued after pressing the OK button

    class Widget(QWidget):
        def __init__(self, *args, **kwargs):
            QWidget.__init__(self, *args, **kwargs)
            self.color_chooser = QColorDialog()
            self.color_chooser.colorSelected.connect(self.color_pick)
            self.color_chooser.show()
    
        def color_pick(self, color):
            print('signaled', color)
    

    If you want to get the color after pressing the OK button you can use these other methods:

    class Widget(QWidget):
        def __init__(self, *args, **kwargs):
            QWidget.__init__(self, *args, **kwargs)
            self.color_chooser = QColorDialog()
            if self.color_chooser.exec_() == QColorDialog.Accepted:
                print(self.color_chooser.currentColor())
    

    class Widget(QWidget):
        def __init__(self, *args, **kwargs):
            QWidget.__init__(self, *args, **kwargs)
            self.color_chooser = QColorDialog()
            color = self.color_chooser.getColor()
            if color.isValid():
                print(color, color.name())