问题描述
是否可以显示QFileDialog,用户可以在其中选择文件或目录(可以选择一个文件或目录)?
Is it possible to show a QFileDialog where the user can select a file or a directory, either one?
QFileDialog::getOpenFileName()
仅接受文件,而QFileDialog::getExistingDirectory()
仅接受目录,但是我需要显示一个文件对话框,该对话框可以接受文件或目录(这对我的程序很有意义). QFileDialog::Options
没有什么希望.
QFileDialog::getOpenFileName()
accepts only files, while QFileDialog::getExistingDirectory()
is directories-only, but I need to show a file dialog that can accept both a file or a directory (it makes sense for my program). QFileDialog::Options
didn't have anything promising.
推荐答案
QFileDialog当前不支持此功能.我认为这里的主要问题是 FileMode 不是 Q_FLAGS ,其值为也不是2的幂,因此,您无法编写此代码来解决此问题.
QFileDialog currently does not support this. I think the main problem for you here is that the FileMode is not a Q_FLAGS and the values are not power of 2, either, and so, you cannot write this to solve this issue.
setFileMode(QFileDialog::Directory|QFileDialog::ExistingFiles)
要解决此问题,您需要进行一些摆弄,例如:
To solve this, you would need quite a bit of fiddling, e.g.:
-
覆盖打开按钮的点击操作.
Override the open button click operation.
正确获取文件和目录的"treeview"索引.
Get the "treeview" indices properly for both files and directories.
我在下面的尝试演示了前一种方法,但是我并没有真正解决第二种方法,因为这似乎涉及到对选择模型的更多摆弄.
My attempt below demonstrates the former, but I did not really go as far as solving the second because that seems to involve some more fiddling with the selection model.
#include <QFileDialog>
#include <QApplication>
#include <QWidget>
#include <QTreeWidget>
#include <QPushButton>
#include <QStringList>
#include <QModelIndex>
#include <QDir>
#include <QDebug>
class FileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = Q_NULLPTR)
: QFileDialog(parent)
{
setOption(QFileDialog::DontUseNativeDialog);
setFileMode(QFileDialog::Directory);
// setFileMode(QFileDialog::ExistingFiles);
for (auto *pushButton : findChildren<QPushButton*>()) {
qDebug() << pushButton->text();
if (pushButton->text() == "&Open" || pushButton->text() == "&Choose") {
openButton = pushButton;
break;
}
}
disconnect(openButton, SIGNAL(clicked(bool)));
connect(openButton, &QPushButton::clicked, this, &FileDialog::openClicked);
treeView = findChild<QTreeView*>();
}
QStringList selected() const
{
return selectedFilePaths;
}
public slots:
void openClicked()
{
selectedFilePaths.clear();
qDebug() << treeView->selectionModel()->selection();
for (const auto& modelIndex : treeView->selectionModel()->selectedIndexes()) {
qDebug() << modelIndex.column();
if (modelIndex.column() == 0)
selectedFilePaths.append(directory().absolutePath() + modelIndex.data().toString());
}
emit filesSelected(selectedFilePaths);
hide();
qDebug() << selectedFilePaths;
}
private:
QTreeView *treeView;
QPushButton *openButton;
QStringList selectedFilePaths;
};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication application(argc, argv);
FileDialog fileDialog;
fileDialog.show();
return application.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT += widgets
CONFIG += c++11
SOURCES += main.cpp
构建并运行
qmake && make && ./main
这篇关于接受单个文件或单个目录的QFileDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!