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?
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?
Calling setStylesheet
on an instance of QFileDialog
has no effect when you use the static functions. Those functions will create their own internal file-dialog, and so the stylesheet will be ignored.
If you want to use your own stylesheet, you will need to use the file-dialog instance you created:
dial = QFileDialog()
dial.setStyleSheet(self.styleSheet())
dial.setWindowTitle('Specify File')
dial.setFileMode(QFileDialog.ExistingFile)
if dial.exec_() == QFileDialog.Accepted:
path = dial.selectedFiles()[0]
However, this may mean that you get Qt's built-in file-dialog, rather than your platform's native file-dialog.
PS:
If you do get the native file-dialog and the stylesheet has no effect on it, the only work-around will be to fallback to Qt's built-in file-dialog. To do that, just add this line:
dial.setOption(QFileDialog.DontUseNativeDialog)
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
Also for C++ version , the DontUseNativeDialog option works fine.
QString text = QFileDialog::getOpenFileName(parent,
tr("title message"),
folder_path_string,
tr("filter (*.extension)"),
nullptr,
QFileDialog::DontUseNativeDialog);