Search code examples
pythonqtpyqtpysideqfiledialog

How to set stylesheet on QFileDialog?


I'm trying to set my QFileDialog style sheet but is has not effect. Here is the code:

dial = QFileDialog()
dial.setStyleSheet(self.styleSheet())
path = dial.getOpenFileName(self, "Specify File")

Any ideas why this don't work?


Solution

  • I recommend to always set parents and use the inheritance of style sheets wherever possible. That way you can also use the static functions of QFileDialog.

    I can confirm ekhumoros suspicion that the native file-dialog ignores the stylesheet. It indeed does on Windows.

    Here the example using the Qt's built-in file-dialog.

    from PyQt5 import QtWidgets
    
    def show_file_dialog():
        QtWidgets.QFileDialog.getOpenFileName(b, options=QtWidgets.QFileDialog.DontUseNativeDialog)
    
    app = QtWidgets.QApplication([])
    
    b = QtWidgets.QPushButton('Test')
    b.setStyleSheet("QWidget { background-color: yellow }")
    b.clicked.connect(show_file_dialog)
    b.show()
    
    app.exec_()
    

    which looks like

    enter image description here