Issue
I am trying to make a browser button in PyQt5 and hence I tried reading the documentation for Qt regarding getOpenFileName. Here is an script of the documentation:
QString QFileDialog::getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(),
const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr,
QFileDialog::Options options = Options())
What does the *
mean in QWidget *parent
or QString *selectedFilter = nullptr
?
What does &
mean in const QString &caption = QString()
Do these *,&
apply for Qt use in C? or do they also apply if I use Python to use Qt?
I understand that:
The first argument is the parent class which also can be set to
none
The second argument is a string which will be used as the caption of the window
const QString &filter = QString()
means the filter to select files.
However, I do not understand QString *selectedFilter = nullptr
. According to the documentation:
The filter selected is set to selectedFilter.
Is it the default filter? Then how to implement it? When I experiment it seems the first filter is the default one and hence it seems there is no need for this option!
Solution
In general translating a function from Qt to PyQt5 is trivial as you show with some of the arguments but others must understand the logic of the function. That is the case of the "selectedFilter" argument that returns the filter used in the selection of the file. In C ++, unlike Python, you can only return a value so a trick to return other values is to use pointers, and that is the current case, but in python you can return a tuple, so it is handled in this case :
filename, selectedFilter = QtWidgets.QFileDialog.getOpenFileName(...)
On the other hand, a simpler way to handle the PyQt5 docs is to use the python "help":
$ python
>>> from PyQt5 import QtWidgets
>>> help(QtWidgets.QFileDialog.getOpenFileName)
Output:
Help on built-in function getOpenFileName:
getOpenFileName(...)
getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[str, str]
Where it shows that it returns a tuple of 2 strings that are the filename of the selected file and the selected filter.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.