📄 qfiledialog.cpp
字号:
int r = entries.indexOf(filename); if (r >= 0) index = d->model->index(r, 0, d->rootIndex()); } if (index.isValid()) { d->selections->select(index, QItemSelectionModel::Select|QItemSelectionModel::Rows); } else { d->selections->clear(); d->fileNameEdit->setText(text); }}/*! Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles, selectedFiles() is an empty string list. \sa selectedFilter()*/QStringList QFileDialog::selectedFiles() const{ Q_D(const QFileDialog); QModelIndexList indexes = d->selections->selectedIndexes(); QStringList files; int r = -1; for (int i = 0; i < indexes.count(); ++i) { QModelIndex index = indexes.at(i); if (index.row() != r && index.column() == 0) { files.append(d->model->filePath(index)); r = index.row(); } } // if we have no selected items, use the name(s) in the lineedit if (files.isEmpty() && !d->fileNameEdit->text().isEmpty()) { QString editText = d->fileNameEdit->text(); if (editText.contains('"')) { // " is used to separate files like so: "file1" "file2" "file3" ... // ### need escape character for filenames with quotes (") QStringList tokens = editText.split("\""); for (int i = 0; i < tokens.size(); ++i) { if ((i % 2) == 0) continue; // Every even token is a separator QString name = d->toInternal(tokens.at(i)); QFileInfo info(name); // if the filename has no suffix, add the default suffix if (!d->defaultSuffix.isEmpty() && !info.isDir() && name.lastIndexOf('.') == -1) name += "." + d->defaultSuffix; // a new filename if ((d->fileMode == ExistingFiles) || files.isEmpty()) { if (info.isAbsolute()) files.append(name); else files.append(d->toInternal(d->lookInCombo->currentText() + QDir::separator() + name)); } } } else { QString name = editText; QFileInfo info(name); // if the filename has no suffix, add the default suffix if (!d->defaultSuffix.isEmpty() && !info.isDir() && name.lastIndexOf('.') == -1) name += "." + d->defaultSuffix; if (info.isAbsolute()) files.append(name); else files.append(d->toInternal(d->lookInCombo->currentText() + QDir::separator() + name)); } } // accept the current directory when in DirectoryOnly mode if (files.isEmpty() && d->fileNameEdit->text().isEmpty() && d->fileMode == DirectoryOnly) files.append(d->model->filePath(d->rootIndex())); return files;}/*! Sets the filter used in the file dialog to the given \a filter. If \a filter contains a pair of parentheses containing one or more of \bold{anything*something}, separated by spaces or by semicolons then only the text contained in the parentheses is used as the filter. This means that these calls are all equivalent: \code fd->setFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)"); fd->setFilter("*.cpp *.cc *.C *.cxx *.c++"); fd->setFilter("All C++ files (*.cpp;*.cc;*.C;*.cxx;*.c++)"); fd->setFilter("*.cpp;*.cc;*.C;*.cxx;*.c++"); \endcode \sa setFilters()*/void QFileDialog::setFilter(const QString &filter){ Q_D(QFileDialog); d->fileTypeCombo->clear(); d->fileTypeCombo->addItem(filter); d->_q_useFilter(filter);}/*! Sets the \a filters used in the file dialog. \code QStringList types; types << "Image files (*.png *.xpm *.jpg)" << "Text files (*.txt)" << "Any files (*)"; QFileDialog fd = new QFileDialog( this ); fd->setFilters( types ); fd->show(); \endcode*/void QFileDialog::setFilters(const QStringList &filters){ Q_D(QFileDialog); d->fileTypeCombo->clear(); d->fileTypeCombo->addItems(filters); d->_q_useFilter(filters.first());}/*! Returns the file type filters that are in operation on this file dialog.*/QStringList QFileDialog::filters() const{ Q_D(const QFileDialog); QStringList items; for (int i = 0; i < d->fileTypeCombo->count(); ++i) items.append(d->fileTypeCombo->itemText(i)); return items;}/*! Sets the current file type \a filter. Multiple filters can be passed in \a filter by separating them with semicolons or spaces. \sa setFilter() setFilters()*/void QFileDialog::selectFilter(const QString &filter){ Q_D(QFileDialog); int i = d->fileTypeCombo->findText(filter); if (i >= 0) { d->fileTypeCombo->setCurrentIndex(i); // emits currentIndexChanged, but we connect to activated d->_q_useFilter(d->fileTypeCombo->currentText()); // so we make sure that _q_useFilter gets called }}/*! Returns the filter that the user selected in the file dialog. \sa selectedFiles()*/QString QFileDialog::selectedFilter() const{ return d_func()->fileTypeCombo->currentText();}/*! \property QFileDialog::viewMode \brief the way files and directories are displayed in the dialog By default, the \c Detail mode is used to display information about files and directories. \sa ViewMode*/void QFileDialog::setViewMode(ViewMode mode){ if (mode == Detail) d_func()->_q_showDetails(); else d_func()->_q_showList();}QFileDialog::ViewMode QFileDialog::viewMode() const{ Q_D(const QFileDialog); return d->viewMode();}/*! \property QFileDialog::fileMode \brief the file mode of the dialog The file mode defines the number and type of items that the user is expected to select in the dialog. \sa FileMode*/void QFileDialog::setFileMode(FileMode mode){ Q_D(QFileDialog); d->fileMode = mode; // set selection mode and behavior QAbstractItemView::SelectionMode selectionMode = d->selectionMode(mode); d->listView->setSelectionMode(selectionMode); d->listView->setSelectionBehavior(QAbstractItemView::SelectRows); d->treeView->setSelectionMode(selectionMode); d->treeView->setSelectionBehavior(QAbstractItemView::SelectRows); // set filter QDir::Filters filters = d->filterForMode(mode); d->model->setFilter(filters); // setup file type for directory if (mode == DirectoryOnly) { d->fileTypeCombo->clear(); d->fileTypeCombo->addItem(tr("Directories")); setLabelText(FileName, tr("Directory:")); } else { setLabelText(FileName, tr("File name:")); } d->fileTypeCombo->setEnabled(mode != DirectoryOnly); d->model->refresh(d->rootIndex());}QFileDialog::FileMode QFileDialog::fileMode() const{ return d_func()->fileMode;}/*! \property QFileDialog::acceptMode \brief the accept mode of the dialog The action mode defines whether the dialog is for opening or saving files. \sa AcceptMode*/void QFileDialog::setAcceptMode(AcceptMode mode){ Q_D(QFileDialog); d->acceptMode = mode; d->openAction->setText(mode == AcceptOpen ? tr("&Open") : tr("&Save")); d->acceptButton->setText(mode == AcceptOpen ? tr("Open") : tr("Save"));}QFileDialog::AcceptMode QFileDialog::acceptMode() const{ return d_func()->acceptMode;}/*! \property QFileDialog::readOnly \brief Wether the filedialog is readonly. If this property is set to false, the filedialog will allow creating, renaming, copying and deleting files and directories.*/void QFileDialog::setReadOnly(bool enabled){ d_func()->model->setReadOnly(enabled);}bool QFileDialog::isReadOnly() const{ return d_func()->model->isReadOnly();}/*! \property QFileDialog::resolveSymlinks \brief whether the filedialog should resolve symbolic links If this property is set to true, the file dialog will resolve symbolic links.*/void QFileDialog::setResolveSymlinks(bool enabled){ d_func()->model->setResolveSymlinks(enabled);}bool QFileDialog::resolveSymlinks() const{ return d_func()->model->resolveSymlinks();}/*! \property QFileDialog::confirmOverwrite \brief whether the filedialog should ask before accepting a selected file, when the accept mode is AcceptSave If this property is set to true and the accept mode is AcceptSave, the filedialog will ask whether the user wants to overwrite the fike before accepting the file.*/void QFileDialog::setConfirmOverwrite(bool enabled){ d_func()->confirmOverwrite = enabled;}bool QFileDialog::confirmOverwrite() const{ return d_func()->confirmOverwrite;}/*! \property QFileDialog::defaultSuffix \brief suffix added to the filename if no other suffix was specified This property specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).*/void QFileDialog::setDefaultSuffix(const QString &suffix){ d_func()->defaultSuffix = suffix;}QString QFileDialog::defaultSuffix() const{ return d_func()->defaultSuffix;}/*! Sets the browsing history of the filedialog to contain the given \a paths.*/void QFileDialog::setHistory(const QStringList &paths){ Q_D(QFileDialog); QStringList::const_iterator it = paths.constBegin(); for (; it != paths.constEnd(); ++it) { QModelIndex index = d->model->index(*it); QIcon icn = d->model->fileIcon(index); d->lookInCombo->addItem(icn, *it); } d->history = paths; d->backButton->setEnabled(!d->history.isEmpty());}/*! \brief returns the browsing history of the filedialog as a list of paths.*/QStringList QFileDialog::history() const{ Q_D(const QFileDialog); return d->history;}/*! \brief set the item delegate used to render the items in the views in the filedialog to the specified \a delegate*/void QFileDialog::setItemDelegate(QAbstractItemDelegate *delegate){ Q_D(QFileDialog); d->listView->setItemDelegate(delegate); d->treeView->setItemDelegate(delegate);}/*! \brief returns the item delegate used to render the items in the views in the filedialog*/QAbstractItemDelegate *QFileDialog::itemDelegate() const{ return d_func()->listView->itemDelegate();}/*! \brief set the icon provider used by the filedialog to the specified \a provider*/void QFileDialog::setIconProvider(QFileIconProvider *provider){ d_func()->model->setIconProvider(provider);}/*! \brief returns the icon provider used by the filedialog.*/QFileIconProvider *QFileDialog::iconProvider() const{ return d_func()->model->iconProvider();}/*! \brief set the \a text shown in the filedialog in the specified \a label*/void QFileDialog::setLabelText(DialogLabel label, const QString &text){ Q_D(QFileDialog); switch (label) { case LookIn: d->lookInLabel->setText(text); break; case FileName: d->fileNameLabel->setText(text); break; case FileType: d->fileTypeLabel->setText(text); break; case Accept: d->acceptButton->setText(text); break; case Reject: d->rejectButton->setText(text); break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -