1

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?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Dariusz
  • 960
  • 13
  • 36
  • 1
    Can you explain what happens when you execute the above code? Do you receive an error of some sort? – Noah Christopher Nov 22 '16 at 02:29
  • Nope, the getOpenFileName window is white - standard windows, where as I wanted it to be dark gray with some other changes. The style has background-color/color and other parameters in it specified. Just trying to get the theme of app constant and I could not ever edit the QFIleDialog look so just though its time to ask... – Dariusz Nov 22 '16 at 02:34

3 Answers3

2

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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • 1
    Instead of `dial.setStyleSheet(self.styleSheet())` why not just using the parent and the inheritance of style sheets. `dial=QFileDialog(self)`? – NoDataDumpNoContribution Nov 24 '16 at 08:47
  • @Trilarion. Because I just didn't think of it :) I suppose I got distracted by the native file-dialog issue. – ekhumoro Nov 24 '16 at 16:20
  • Hey, turns out all I needed was `QFileDialog.getOpenFileName(self, options=QFileDialog.DontUseNativeDialog)`, Thanks! – Dariusz Nov 24 '16 at 16:32
2

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

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
0

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