Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - Difficulty in understanding the Qt documentation

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:

  1. The first argument is the parent class which also can be set to none

  2. The second argument is a string which will be used as the caption of the window

  3. 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...