📄 qfiledialog.cpp
字号:
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(QFileDialog::FileMode mode){ Q_D(QFileDialog); d->fileMode = mode; d->retranslateWindowTitle(); // set selection mode and behavior QAbstractItemView::SelectionMode selectionMode; if (mode == QFileDialog::ExistingFiles) selectionMode = QAbstractItemView::ExtendedSelection; else selectionMode = QAbstractItemView::SingleSelection; d->qFileDialogUi->listView->setSelectionMode(selectionMode); d->qFileDialogUi->treeView->setSelectionMode(selectionMode); // set filter QDir::Filters filters = d->filterForMode(mode); d->model->setFilter(filters); // setup file type for directory QString buttonText = (d->acceptMode == AcceptOpen ? tr("&Open") : tr("&Save")); if (mode == DirectoryOnly || mode == Directory) { d->qFileDialogUi->fileTypeCombo->clear(); d->qFileDialogUi->fileTypeCombo->addItem(tr("Directories")); d->qFileDialogUi->fileTypeCombo->setEnabled(false); setLabelText(FileName, tr("Directory:")); buttonText = tr("&Choose"); } else { setLabelText(FileName, tr("File &name:")); } setLabelText(Accept, buttonText); d->qFileDialogUi->fileTypeCombo->setEnabled(mode != DirectoryOnly); d->_q_updateOkButton();}QFileDialog::FileMode QFileDialog::fileMode() const{ Q_D(const QFileDialog); return d->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(QFileDialog::AcceptMode mode){ Q_D(QFileDialog); d->acceptMode = mode; bool directoryMode = (d->fileMode == Directory || d->fileMode == DirectoryOnly); QDialogButtonBox::StandardButton button = (mode == AcceptOpen ? QDialogButtonBox::Open : QDialogButtonBox::Save); d->qFileDialogUi->buttonBox->setStandardButtons(button | QDialogButtonBox::Cancel); d->qFileDialogUi->buttonBox->button(button)->setEnabled(false); d->_q_updateOkButton(); if (mode == AcceptOpen && directoryMode) setLabelText(Accept, tr("&Choose")); else setLabelText(Accept, (mode == AcceptOpen ? tr("&Open") : tr("&Save"))); if (mode == AcceptSave) { d->qFileDialogUi->lookInCombo->setEditable(false); }}/* Returns the file system model index that is the root index in the views*/QModelIndex QFileDialogPrivate::rootIndex() const { return mapToSource(qFileDialogUi->listView->rootIndex());}QAbstractItemView *QFileDialogPrivate::currentView() const { if (!qFileDialogUi->stackedWidget) return 0; if (qFileDialogUi->stackedWidget->currentWidget() == qFileDialogUi->listView->parent()) return qFileDialogUi->listView; return qFileDialogUi->treeView;}QLineEdit *QFileDialogPrivate::lineEdit() const { return (QLineEdit*)qFileDialogUi->fileNameEdit;}/* Sets the view root index to be the file system model index*/void QFileDialogPrivate::setRootIndex(const QModelIndex &index) const { Q_ASSERT(index.isValid() ? index.model() == model : true); QModelIndex idx = mapFromSource(index); qFileDialogUi->treeView->setRootIndex(idx); qFileDialogUi->listView->setRootIndex(idx);}/* Select a file system model index returns the index that was selected (or not depending upon sortfilterproxymodel)*/QModelIndex QFileDialogPrivate::select(const QModelIndex &index) const { Q_ASSERT(index.isValid() ? index.model() == model : true); QModelIndex idx = mapFromSource(index); if (idx.isValid()) qFileDialogUi->listView->selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows); return idx;}QFileDialog::AcceptMode QFileDialog::acceptMode() const{ Q_D(const QFileDialog); return d->acceptMode;}/*! \property QFileDialog::readOnly \brief Whether the filedialog is readonly. If this property is set to false, the filedialog will allow renaming, and deleting of files and directories and creating directories. The default value is false.*/void QFileDialog::setReadOnly(bool disabled){ Q_D(QFileDialog); d->model->setReadOnly(disabled); if (disabled) { d->qFileDialogUi->newFolderButton->setEnabled(false); d->renameAction->setEnabled(false); d->deleteAction->setEnabled(false); }}bool QFileDialog::isReadOnly() const{ Q_D(const QFileDialog); return d->model->isReadOnly();}/*! \property QFileDialog::resolveSymlinks \brief whether the filedialog should resolve shortcuts If this property is set to true, the file dialog will resolve shortcuts.*/void QFileDialog::setResolveSymlinks(bool enabled){ Q_D(QFileDialog); d->model->setResolveSymlinks(enabled);}bool QFileDialog::resolveSymlinks() const{ Q_D(const QFileDialog); return d->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 file before accepting the file.*/void QFileDialog::setConfirmOverwrite(bool enabled){ Q_D(QFileDialog); d->confirmOverwrite = enabled;}bool QFileDialog::confirmOverwrite() const{ Q_D(const QFileDialog); return d->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){ Q_D(QFileDialog); d->defaultSuffix = suffix;}QString QFileDialog::defaultSuffix() const{ Q_D(const QFileDialog); return d->defaultSuffix;}/*! Sets the browsing history of the filedialog to contain the given \a paths.*/void QFileDialog::setHistory(const QStringList &paths){ Q_D(QFileDialog); d->history = paths; d->urlModel->setUrls(QList<QUrl>()); QList<QUrl> list; QModelIndex idx = d->model->index(d->rootPath()); while (idx.isValid()) { QUrl url = QUrl::fromLocalFile(d->model->filePath(idx)); if (url.isValid()) list.append(url); idx = idx.parent(); } // add "my computer" list.append(QUrl::fromLocalFile(QLatin1String(""))); d->urlModel->addUrls(list, 0); idx = d->qFileDialogUi->lookInCombo->model()->index(d->qFileDialogUi->lookInCombo->model()->rowCount() - 1, 0); // ### append history QList<QUrl> urls; for (int i = 0; i < d->history.count(); ++i) { QUrl path = QUrl::fromLocalFile(d->history.at(i)); if (!urls.contains(path)) urls += path; } if (urls.count() > 0) { d->qFileDialogUi->lookInCombo->model()->insertRow(d->qFileDialogUi->lookInCombo->model()->rowCount()); idx = d->qFileDialogUi->lookInCombo->model()->index(d->qFileDialogUi->lookInCombo->model()->rowCount()-1, 0); // ### TODO disable this item and maybe add a horizontal line before it d->qFileDialogUi->lookInCombo->model()->setData(idx, QLatin1String("Recent Places")); QStandardItemModel *m = qobject_cast<QStandardItemModel*>(d->qFileDialogUi->lookInCombo->model()); if (m) { Qt::ItemFlags flags = m->flags(idx); flags &= ~Qt::ItemIsEnabled; m->item(idx.row(), idx.column())->setFlags(flags); } d->urlModel->addUrls(urls, -1, false); } d->qFileDialogUi->lookInCombo->setCurrentIndex(0);}/*! \brief returns the browsing history of the filedialog as a list of paths.*/QStringList QFileDialog::history() const{ Q_D(const QFileDialog); QStringList currentHistory = d->history; QString newHistory = d->model->filePath(d->rootIndex()); if (!currentHistory.contains(newHistory)) currentHistory << newHistory; return currentHistory;}/*! Sets the item delegate used to render items in the views in the file dialog to the given \a delegate. \warning You should not share the same instance of a delegate between views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the \l{QAbstractItemDelegate::}{closeEditor()} signal, and attempt to access, modify or close an editor that has already been closed.*/void QFileDialog::setItemDelegate(QAbstractItemDelegate *delegate){ Q_D(QFileDialog); d->qFileDialogUi->listView->setItemDelegate(delegate); d->qFileDialogUi->treeView->setItemDelegate(delegate);}/*! \brief returns the item delegate used to render the items in the views in the filedialog*/QAbstractItemDelegate *QFileDialog::itemDelegate() const{ Q_D(const QFileDialog); return d->qFileDialogUi->listView->itemDelegate();}/*! \brief set the icon provider used by the filedialog to the specified \a provider*/void QFileDialog::setIconProvider(QFileIconProvider *provider){ Q_D(QFileDialog); d->model->setIconProvider(provider);}/*! \brief returns the icon provider used by the filedialog.*/QFileIconProvider *QFileDialog::iconProvider() const{ Q_D(const QFileDialog); return d->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); QPushButton *button; switch (label) { case LookIn: d->qFileDialogUi->lookInLabel->setText(text); break; case FileName: d->qFileDialogUi->fileNameLabel->setText(text); break; case FileType: d->qFileDialogUi->fileTypeLabel->setText(text); break; case Accept: if (acceptMode() == AcceptOpen) button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Open); else button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Save); if (button) button->setText(text); break; case Reject: button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Cancel); if (button) button->setText(text); break; }}/*! \brief returns the text shown in the filedialog in the specified \a label*/QString QFileDialog::labelText(DialogLabel label) const{ QPushButton *button; Q_D(const QFileDialog); switch (label) { case LookIn: return d->qFileDialogUi->lookInLabel->text(); case FileName: return d->qFileDialogUi->fileNameLabel->text(); case FileType: return d->qFileDialogUi->fileTypeLabel->text(); case Accept: if (acceptMode() == AcceptOpen) button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Open); else button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Save); if (button) return button->text(); case Reject: button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Cancel); if (button) return button->text(); } return QString();}/* For the native file dialogs*/#if defined(Q_WS_WIN)extern QString qt_win_get_open_file_name(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -