如何制作"Browse for folder"对话框?
How to make "Browse for folder" dialog?
我正在尝试制作一个如下所示的文件夹选择对话框:
图片来自这个线程:can the Open File dialog be used to select a Folder?
我尝试了 QFileDialog::getExistingDirectory()
并制作了 QFileDialog 的实例并设置了属性。它只显示带有隐藏文件的“打开文件”对话框。
试试这个:QFileDialog::getExistingDirectory()
- http://qt-project.org/doc/qt-4.8/qfiledialog.html#getExistingDirectory
- http://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
两种静态方法都以目录选择模式打开文件选择器。如果您不喜欢打开的对话框的外观,您需要实现自己的。默认情况下,如果可能,Qt 会尝试打开本机,这在 99.9% 的情况下应该有效。
看来还是得自己回答了。
最后我完全在 Qt 中完成了整个对话框:
头文件:
#pragma once
#include <QtWidgets/QDialog>
class QTreeView;
class QFileSystemModel;
class QLineEdit;
class QPushButton;
class CDirSelectionDlg : public QDialog {
Q_OBJECT
public:
CDirSelectionDlg(const QString initialPath, QWidget *parent = nullptr);
QDir directory() const;
private:
void onCurrentChanged();
QTreeView *m_treeView;
QFileSystemModel *m_model;
QLineEdit *m_folderName;
QPushButton *m_OKbutton;
QString m_initialPath;
};
源文件:
#include "DirSelectionDlg.h"
#include <QLabel>
#include <QBoxLayout>
#include <QDialogButtonBox>
#include <QTreeView>
#include <QFileSystemModel>
#include <QPushButton>
#include <QLineEdit>
CDirSelectionDlg::CDirSelectionDlg(const QString initialPath, QWidget *parent) : QDialog(parent), m_initialPath(initialPath)
{
setMinimumSize(200, 300);
resize(400, 430);
m_model = new QFileSystemModel(this);
auto rootIdx = m_model->setRootPath(m_initialPath);
m_treeView = new QTreeView(this);
m_treeView->setModel(m_model);
m_treeView->setSelectionMode(QAbstractItemView::SingleSelection);
m_treeView->setHeaderHidden(true);
m_treeView->setSortingEnabled(true);
m_treeView->sortByColumn(0, Qt::AscendingOrder);
for(int i = 1; i < m_model->columnCount(); i ++) // don't show Size, Type, etc.
m_treeView->setColumnHidden(i, true);
m_treeView->scrollTo(rootIdx);
m_treeView->selectionModel()->setCurrentIndex(rootIdx, QItemSelectionModel::Current | QItemSelectionModel::Select);
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CDirSelectionDlg::onCurrentChanged);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, &QDialogButtonBox::accepted, this, &CDirSelectionDlg::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &CDirSelectionDlg::reject);
m_OKbutton = buttonBox->button(QDialogButtonBox::Ok);
auto label = new QLabel(tr("Folder:"));
m_folderName = new QLineEdit(this);
m_folderName->setReadOnly(true);
m_folderName->setText(QFileInfo(m_initialPath).fileName());
auto pathLayout = new QHBoxLayout();
pathLayout->addWidget(label);
pathLayout->addSpacing(10);
pathLayout->addWidget(m_folderName);
auto mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_treeView);
mainLayout->addSpacing(10);
mainLayout->addLayout(pathLayout);
mainLayout->addSpacing(10);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
void CDirSelectionDlg::onCurrentChanged()
{
auto fileInfo = m_model->fileInfo(m_treeView->selectionModel()->currentIndex());
m_folderName->setText(fileInfo.fileName());
m_OKbutton->setEnabled(fileInfo.isDir());
m_OKbutton->setDefault(fileInfo.isDir());
}
QDir CDirSelectionDlg::directory() const
{
return QDir(m_model->fileInfo(m_treeView->selectionModel()->currentIndex()).absoluteFilePath());
}
我正在使用 Qt 5.3.1。
我正在尝试制作一个如下所示的文件夹选择对话框:
图片来自这个线程:can the Open File dialog be used to select a Folder?
我尝试了 QFileDialog::getExistingDirectory()
并制作了 QFileDialog 的实例并设置了属性。它只显示带有隐藏文件的“打开文件”对话框。
试试这个:QFileDialog::getExistingDirectory()
- http://qt-project.org/doc/qt-4.8/qfiledialog.html#getExistingDirectory
- http://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
两种静态方法都以目录选择模式打开文件选择器。如果您不喜欢打开的对话框的外观,您需要实现自己的。默认情况下,如果可能,Qt 会尝试打开本机,这在 99.9% 的情况下应该有效。
看来还是得自己回答了。 最后我完全在 Qt 中完成了整个对话框:
头文件:
#pragma once
#include <QtWidgets/QDialog>
class QTreeView;
class QFileSystemModel;
class QLineEdit;
class QPushButton;
class CDirSelectionDlg : public QDialog {
Q_OBJECT
public:
CDirSelectionDlg(const QString initialPath, QWidget *parent = nullptr);
QDir directory() const;
private:
void onCurrentChanged();
QTreeView *m_treeView;
QFileSystemModel *m_model;
QLineEdit *m_folderName;
QPushButton *m_OKbutton;
QString m_initialPath;
};
源文件:
#include "DirSelectionDlg.h"
#include <QLabel>
#include <QBoxLayout>
#include <QDialogButtonBox>
#include <QTreeView>
#include <QFileSystemModel>
#include <QPushButton>
#include <QLineEdit>
CDirSelectionDlg::CDirSelectionDlg(const QString initialPath, QWidget *parent) : QDialog(parent), m_initialPath(initialPath)
{
setMinimumSize(200, 300);
resize(400, 430);
m_model = new QFileSystemModel(this);
auto rootIdx = m_model->setRootPath(m_initialPath);
m_treeView = new QTreeView(this);
m_treeView->setModel(m_model);
m_treeView->setSelectionMode(QAbstractItemView::SingleSelection);
m_treeView->setHeaderHidden(true);
m_treeView->setSortingEnabled(true);
m_treeView->sortByColumn(0, Qt::AscendingOrder);
for(int i = 1; i < m_model->columnCount(); i ++) // don't show Size, Type, etc.
m_treeView->setColumnHidden(i, true);
m_treeView->scrollTo(rootIdx);
m_treeView->selectionModel()->setCurrentIndex(rootIdx, QItemSelectionModel::Current | QItemSelectionModel::Select);
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CDirSelectionDlg::onCurrentChanged);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, &QDialogButtonBox::accepted, this, &CDirSelectionDlg::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &CDirSelectionDlg::reject);
m_OKbutton = buttonBox->button(QDialogButtonBox::Ok);
auto label = new QLabel(tr("Folder:"));
m_folderName = new QLineEdit(this);
m_folderName->setReadOnly(true);
m_folderName->setText(QFileInfo(m_initialPath).fileName());
auto pathLayout = new QHBoxLayout();
pathLayout->addWidget(label);
pathLayout->addSpacing(10);
pathLayout->addWidget(m_folderName);
auto mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_treeView);
mainLayout->addSpacing(10);
mainLayout->addLayout(pathLayout);
mainLayout->addSpacing(10);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
void CDirSelectionDlg::onCurrentChanged()
{
auto fileInfo = m_model->fileInfo(m_treeView->selectionModel()->currentIndex());
m_folderName->setText(fileInfo.fileName());
m_OKbutton->setEnabled(fileInfo.isDir());
m_OKbutton->setDefault(fileInfo.isDir());
}
QDir CDirSelectionDlg::directory() const
{
return QDir(m_model->fileInfo(m_treeView->selectionModel()->currentIndex()).absoluteFilePath());
}
我正在使用 Qt 5.3.1。