3

I use PyQt5 and I have an error when I try to save the file name :

 csv_file_list = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '', '*.csv')
    fileName = csv_file_list 
    fileName = QtWidgets.QFileDialog.getSaveFileName(self, 'Dialog Title', '/path/to/default/directory', selectedFilters='*.txt')
    if fileName:
        print (fileName)

And it display me this error : 'selectedFilters' is not a valid keyword argument.

I don't know if the error is here because of PyQt5 or not

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Did you consider checking the [documentation](http://doc.qt.io/qt-5/qfiledialog.html) to see what the valid arguments are? – ekhumoro Jun 30 '16 at 17:42
  • I check the doc: QString QFileDialog::getSaveFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options()) –  Jul 01 '16 at 07:41
  • But I can't find what's the problem (I try with selectedFilter and selectedFilters too) –  Jul 01 '16 at 07:41

1 Answers1

3

For various reasons, the C++ signatures don't always exactly match the PyQt signatures. This is probably the biggest weakness of PyQt5. There really needs to be a comprehensive reference that details all the differences between the C++ APIs and the PyQt APIs. It sort of exists for PyQt4, in the form of the PyQt Class Reference (which is a partially converted version of the Qt documentation). But there is currently nothing equivalent to that for PyQt5. However, you can always use python's introspection methods to work out the differences for yourself.

Here's the C++ signature for getSaveFileName:

QString getSaveFileName(QWidget *parent = Q_NULLPTR,
                        const QString &caption = QString(),
                        const QString &dir = QString(),
                        const QString &filter = QString(),
                        QString *selectedFilter = Q_NULLPTR,
                        Options options = Options()
                        )

And here's the PyQt5 signature, which was obtained from the help function in a python interactive session - i.e. help(QtWidgets.QFileDialog.getSaveFileName):

getSaveFileName(parent: QWidget = None,
                caption: str = '',
                directory: str = '',
                filter: str = '',
                initialFilter: str = '',
                options: Union[QFileDialog.Options, QFileDialog.Option] = 0,
                ) -> Tuple[str, str]

As you can see, the dir argument has changed to directory, and the selectedFilter argument has changed to initialFilter.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Using the help function for getSaveFileName using PyQt 4 and Python 2.7 gives me "QFileDialog.getOpenFileName(QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, QFileDialog.Options options=0) -> QString". Tried to set the 5th parameter as selectedFilter=aComboBox.getText() but getting the keyword error. Tested initialFilters just to see, but I'm pretty sure it doesn't apply here. Am I incorrectly interpreting the help? – user25976 Dec 17 '16 at 02:26
  • @user25976. What version of PyQt are you using? Support for keyword arguments was added in PyQt-4.7. Did you try to use `QFileDialog.getSaveFileNameAndFilter`? It has a different signature (i.e. with `initialFilter`). – ekhumoro Dec 17 '16 at 02:56
  • I'm using pyqt 4.11.4 so I tried getSaveFileNameAndFilter as you suggested. I am able to return the filter that was selected, but for some reason setting the initialFilter to something like "Format TIFF (*.tiff *TIFF)" (retrieved from a combobox) does not sit the initial filter (just stays on pdf because it's the first one on my filter list)...hm.. – user25976 Dec 20 '16 at 19:50
  • @user25976. It seems to be broken in PyQt4 (but it does work okay in PyQt5). – ekhumoro Dec 20 '16 at 21:23