1

In my PyQt app I'm trying to run native but custom QFileDialog.
My code looks like this.

dlg = QtGui.QFileDialog(self, caption=self.tr("Select a file")))
dlg.setNameFilters("Text files (*.txt)")

if dlg.exec_():
    name = dlg.selectedFiles()

but this shows a Qt dialog, not native. I've tried

dlg.setOption(QtGui.QFileDialog.DontUseNativeDialog, False)

but this didn't help.
How do I run custom native QFileDialog?

2 Answers 2

1

A custom native dialog is a contradiction interms. The main reason for choosing to use a native dialog is so that you can get exactly what the platform normally provides.

But this is a moot point, since getOpenFileName allows you to achieve what you want without any customization:

name = QFileDialog.getOpenFileName(
            parent=self,
            caption=self.tr("Select a file"),
            filter=self.tr('Text files (*.txt)'),
            )
if name:
    print(name)

In addition to the above arguments, there's also dir, which allows you to set the start directory; selectedFilter, which allows you to set the initial filter; and options, which allows you to set one or more Option values (not all of which are relevant for native file-dialogs).

Sign up to request clarification or add additional context in comments.

Comments

1

You can only get the native File Dialogs by using the static functions on QFileDialog. The DontUseNativeDialog option only works when using the static functions.

QFileDialog.getSaveFileName(), etc.

From the Docs

The easiest way to create a QFileDialog is to use the static functions. On Windows, Mac OS X, KDE and GNOME, these static functions will call the native file dialog when possible.

QFileDialog.DontUseNativeDialog   0x00000010  

Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT macro.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.