📄 qfiledialog.cpp
字号:
listView->setSelectionMode(selectionMode(fileMode)); listView->setSelectionBehavior(QAbstractItemView::SelectRows); listView->setRootIndex(current); listView->setWrapping(true); listView->setResizeMode(QListView::Adjust); listView->setEditTriggers(QAbstractItemView::EditKeyPressed); listView->setContextMenuPolicy(Qt::CustomContextMenu);#ifndef QT_NO_DRAGANDDROP listView->setDragEnabled(true);#endif grid->addWidget(listView, 1, 0, 1, 6); QObject::connect(listView, SIGNAL(activated(QModelIndex)), q, SLOT(_q_enterDirectory(QModelIndex))); QObject::connect(listView, SIGNAL(customContextMenuRequested(QPoint)), q, SLOT(_q_showContextMenu(QPoint)));#ifndef QT_NO_SHORTCUT QShortcut *shortcut = new QShortcut(listView); shortcut->setKey(QKeySequence("Delete")); QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));#endif}void QFileDialogPrivate::setupTreeView(const QModelIndex ¤t, QGridLayout *grid){ Q_Q(QFileDialog); treeView = new QFileDialogTreeView(this); treeView->setModel(model); treeView->setSelectionModel(selections); treeView->setSelectionMode(selectionMode(fileMode)); treeView->viewport()->setAcceptDrops(true); treeView->setRootIsDecorated(false); treeView->setItemsExpandable(false); treeView->header()->setStretchLastSection(true); treeView->header()->setSortIndicator(0, Qt::AscendingOrder); treeView->header()->setSortIndicatorShown(true); treeView->header()->setClickable(true); treeView->header()->resizeSection(0, 165); // this si the same size as Stretch ((530 - (100 * 2)) / 2) treeView->setEditTriggers(QAbstractItemView::EditKeyPressed); treeView->setContextMenuPolicy(Qt::CustomContextMenu); treeView->setRootIndex(current); treeView->hide(); grid->addWidget(treeView, 1, 0, 1, 6); QObject::connect(treeView->header(), SIGNAL(sectionClicked(int)), q, SLOT(_q_sortByColumn(int))); QObject::connect(treeView, SIGNAL(activated(QModelIndex)), q, SLOT(_q_enterDirectory(QModelIndex))); QObject::connect(treeView, SIGNAL(customContextMenuRequested(QPoint)), q, SLOT(_q_showContextMenu(QPoint)));#ifndef QT_NO_SHORTCUT QShortcut *shortcut = new QShortcut(treeView); shortcut->setKey(QKeySequence("Delete")); QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));#endif}void QFileDialogPrivate::setupToolButtons(const QModelIndex ¤t, QGridLayout *grid){ Q_Q(QFileDialog); QHBoxLayout *box = new QHBoxLayout; box->setMargin(3); box->setSpacing(3); QSize tools(22, 22); backButton = new QToolButton(q); backButton->setIcon(q->style()->standardPixmap(QStyle::SP_FileDialogBack));#ifndef QT_NO_TOOLTIP backButton->setToolTip(QFileDialog::tr("Back"));#endif backButton->setAutoRaise(true); backButton->setEnabled(false); backButton->setFixedSize(tools); QObject::connect(backButton, SIGNAL(clicked()), q, SLOT(_q_navigateToPrevious())); box->addWidget(backButton); toParentButton = new QToolButton(q); toParentButton->setIcon(q->style()->standardPixmap(QStyle::SP_FileDialogToParent));#ifndef QT_NO_TOOLTIP toParentButton->setToolTip(QFileDialog::tr("Parent Directory"));#endif toParentButton->setAutoRaise(true); toParentButton->setEnabled(model->parent(current).isValid()); toParentButton->setFixedSize(tools); QObject::connect(toParentButton, SIGNAL(clicked()), q, SLOT(_q_navigateToParent())); box->addWidget(toParentButton); newFolderButton = new QToolButton(q); newFolderButton->setIcon(q->style()->standardPixmap(QStyle::SP_FileDialogNewFolder));#ifndef QT_NO_TOOLTIP newFolderButton->setToolTip(QFileDialog::tr("Create New Folder"));#endif newFolderButton->setAutoRaise(true); newFolderButton->setFixedSize(tools); QObject::connect(newFolderButton, SIGNAL(clicked()), q, SLOT(_q_createDirectory())); box->addWidget(newFolderButton); listModeButton = new QToolButton(q); listModeButton->setIcon(q->style()->standardPixmap(QStyle::SP_FileDialogListView));#ifndef QT_NO_TOOLTIP listModeButton->setToolTip(QFileDialog::tr("List View"));#endif listModeButton->setAutoRaise(true); listModeButton->setDown(true); listModeButton->setFixedSize(tools); QObject::connect(listModeButton, SIGNAL(clicked()), q, SLOT(_q_showList())); box->addWidget(listModeButton); detailModeButton = new QToolButton(q); detailModeButton->setIcon(q->style()->standardPixmap(QStyle::SP_FileDialogDetailedView));#ifndef QT_NO_TOOLTIP detailModeButton->setToolTip(QFileDialog::tr("Detail View"));#endif detailModeButton->setAutoRaise(true); detailModeButton->setFixedSize(tools); QObject::connect(detailModeButton, SIGNAL(clicked()), q, SLOT(_q_showDetails())); box->addWidget(detailModeButton); box->setSizeConstraint(QLayout::SetFixedSize); grid->addLayout(box, 0, 4, 1, 2);}void QFileDialogPrivate::setupWidgets(QGridLayout *grid){ // labels Q_Q(QFileDialog); lookInLabel = new QLabel(QFileDialog::tr("Look in:"), q); grid->addWidget(lookInLabel, 0, 0); fileNameLabel = new QLabel(QFileDialog::tr("File name:"), q); grid->addWidget(fileNameLabel, 2, 0); fileTypeLabel = new QLabel(QFileDialog::tr("Files of type:"), q); grid->addWidget(fileTypeLabel, 3, 0); // push buttons acceptButton = new QPushButton(QFileDialog::tr("Open"), q); QObject::connect(acceptButton, SIGNAL(clicked()), q, SLOT(accept())); grid->addWidget(acceptButton, 2, 5, Qt::AlignLeft); rejectButton = new QPushButton(QFileDialog::tr("Cancel"), q); QObject::connect(rejectButton, SIGNAL(clicked()), q, SLOT(reject())); grid->addWidget(rejectButton, 3, 5, Qt::AlignLeft); // "lookin" combobox lookInCombo = new QComboBox(q); lookInCombo->setInsertPolicy(QComboBox::NoInsert); lookInCombo->setDuplicatesEnabled(false); lookInCombo->setEditable(true); lookInCombo->setAutoCompletion(false); QObject::connect(lookInCombo, SIGNAL(activated(QString)), q, SLOT(_q_enterDirectory(QString))); lookInEdit = new QFileDialogLineEdit(lookInCombo); QObject::connect(lookInEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_autoCompleteDirectory(QString))); QObject::connect(lookInEdit, SIGNAL(returnPressed()), q, SLOT(_q_enterDirectory())); lookInCombo->setLineEdit(lookInEdit); lookInCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); grid->addWidget(lookInCombo, 0, 1, 1, 3); // filename fileNameEdit = new QFileDialogLineEdit(q); fileNameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); QObject::connect(fileNameEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_autoCompleteFileName(QString))); QObject::connect(fileNameEdit, SIGNAL(returnPressed()), q, SLOT(accept())); grid->addWidget(fileNameEdit, 2, 1, 1, 3); // filetype fileTypeCombo = new QComboBox(q); fileTypeCombo->setDuplicatesEnabled(false); fileTypeCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); fileTypeCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); QObject::connect(fileTypeCombo, SIGNAL(activated(QString)), q, SLOT(_q_useFilter(QString))); grid->addWidget(fileTypeCombo, 3, 1, 1, 3);}void QFileDialogPrivate::updateButtons(const QModelIndex &index){ toParentButton->setEnabled(index.isValid()); backButton->setEnabled(!history.isEmpty()); newFolderButton->setEnabled(!model->isReadOnly()); bool block = lookInCombo->blockSignals(true); if (index.isValid()) { QString pth = toNative(model->filePath(index)); Q_ASSERT(!pth.isEmpty()); // this should be caught by the first if statement QIcon icn = model->fileIcon(index); int i = lookInCombo->findText(pth); if (i > -1) { lookInCombo->setCurrentIndex(i); } else { lookInCombo->addItem(icn, pth); lookInCombo->setCurrentIndex(lookInCombo->count() - 1); } } else { // "My Computer" is already in the list lookInCombo->setCurrentIndex(0); } lookInCombo->blockSignals(block);}void QFileDialogPrivate::setRootIndex(const QModelIndex &index){ Q_Q(QFileDialog); bool block = selections->blockSignals(true); selections->clear(); listView->setRootIndex(index); treeView->setRootIndex(index); model->refresh(index); selections->blockSignals(block); if (fileMode == QFileDialog::DirectoryOnly) fileNameEdit->clear(); else q->selectFile(fileNameEdit->text());}QModelIndex QFileDialogPrivate::rootIndex() const{ return listView->rootIndex();}void QFileDialogPrivate::setDirSorting(QDir::SortFlags sort){ int sortBy = sort & QDir::SortByMask; sortByNameAction->setChecked(sortBy == QDir::Name); sortBySizeAction->setChecked(sortBy == QDir::Size); sortByDateAction->setChecked(sortBy == QDir::Time); unsortedAction->setChecked(sortBy == QDir::Unsorted); model->setSorting(sort); model->refresh(rootIndex());}void QFileDialogPrivate::setDirFilter(QDir::Filters filters){ showHiddenAction->setChecked(filters & QDir::Hidden); model->setFilter(filters); model->refresh(rootIndex());}QDir::Filters QFileDialogPrivate::filterForMode(QFileDialog::FileMode mode){ QDir::Filters f = QDir::Drives | QDir::AllDirs; if (mode != QFileDialog::DirectoryOnly) f |= QDir::Files; return f;}QAbstractItemView::SelectionMode QFileDialogPrivate::selectionMode(QFileDialog::FileMode mode){ if (mode == QFileDialog::ExistingFiles) return QAbstractItemView::ExtendedSelection; return QAbstractItemView::SingleSelection;}QModelIndex QFileDialogPrivate::matchDir(const QString &text, const QModelIndex &first) const{ QModelIndexList matches = model->match(first, Qt::DisplayRole, text, model->rowCount(first.parent()), Qt::MatchStartsWith|Qt::MatchCaseSensitive|Qt::MatchWrap); for (int i = 0; i < matches.count(); ++i) if (model->isDir(matches.at(i))) return matches.at(i); return QModelIndex();}QModelIndex QFileDialogPrivate::matchName(const QString &name, const QModelIndex &first) const{ QModelIndexList matches = model->match(first, Qt::DisplayRole, name, 1, Qt::MatchStartsWith|Qt::MatchCaseSensitive|Qt::MatchWrap); if (matches.count() <= 0) return QModelIndex(); return matches.first();}bool QFileDialogPrivate::itemViewKeyboardEvent(QKeyEvent *e){ switch (e->key()) { case Qt::Key_Backspace: _q_navigateToParent(); return true; case Qt::Key_Back:#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) return false;#endif case Qt::Key_Left: if (e->key() == Qt::Key_Back || e->modifiers() == Qt::AltModifier) { _q_navigateToPrevious(); return true; } break; default: break; } return false;}QString QFileDialogPrivate::getEnvironmentVariable(const QString &str){#ifdef Q_OS_UNIX if (str.size() > 1 && str.startsWith(QLatin1Char('$'))) { return QString::fromLocal8Bit(getenv(str.mid(1).toLatin1().constData())); }#else if (str.size() > 2 && str.startsWith(QLatin1Char('%')) && str.endsWith(QLatin1Char('%'))) { return QString::fromLocal8Bit(getenv(str.mid(1, str.size() - 2).toLatin1().constData())); }#endif return str;}/****************************************************************** * * Static functions for the native filedialogs * ******************************************************************/#include <qapplication.h>#include <qstyle.h>#if defined(Q_WS_WIN)extern QString qt_win_get_open_file_name(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);extern QString qt_win_get_save_file_name(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);extern QStringList qt_win_get_open_file_names(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);extern QString qt_win_get_existing_directory(const QFileDialogArgs &args);#elif defined(Q_WS_MAC)extern QStringList qt_mac_get_open_file_names(const QFileDialogArgs &args, QString *pwd, QString *selectedFilter);extern QString qt_mac_get_save_file_name(const QFileDialogArgs &args, QString *pwd, QString *selectedFilter);#endifQString QFileDialogPrivate::workingDirectory(const QString &path){ if (!path.isEmpty()) { QFileInfo info(path); if (info.exists() && info.isDir()) return path; return info.absolutePath(); } return QDir::currentPath();}QString QFileDialogPrivate::initialSelection(const QString &path){ if (!path.isEmpty()) { QFileInfo info(path); if (!info.isDir()) return info.fileName(); } return QString();}/*! This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string. \code QString s = QFileDialog::getOpenFileName( this, "Choose a file to open", "/home", "Images (*.png *.xpm *.jpg)"); \endcode The function creates a modal file dialog with the given \a parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget. The file dialog's working directory will be set to \a dir. If \a dir includes a file name, the file will be selected. Only files that match the given \a filter are shown. The filter selected is set to \a selectedFilter. The parameters \a dir, \a selectedFilter, and \a filter may be empty strings. The \a options argument holds various options about how to run the dialog, see the QFileDialog::Option enum for more information on the flags you can pass. The dialog's caption is set to \a caption. If \a caption is not specified then a default caption will be used. Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. Note that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just under the parent's title bar. Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, the file dialog will change to \c{/var/tmp} after entering \c{/usr/tmp}. If \a options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories. \sa getOpenFileNames(), getSaveFileName(), getExistingDirectory()*/QString QFileDialog::getOpenFileName(QWidget *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -