【问题标题】:How to make "Browse for folder" dialog?如何制作“浏览文件夹”对话框?
【发布时间】:2015-01-22 11:21:31
【问题描述】:

我正在尝试制作一个如下所示的文件夹选择对话框:

图片来自这个帖子:can the Open File dialog be used to select a Folder?

我尝试了QFileDialog::getExistingDirectory() 和创建 QFileDialog 的实例并设置属性。它只是显示带有隐藏文件的“打开文件”对话框。

【问题讨论】:

  • 您的问题与您提到的问题有何不同?
  • 完全相反。提到的问题的作者得到了像图像上一样的“浏览文件夹”对话框,并且想要我得到的那个。
  • 我猜你无法从 Qt 获得你想要的对话框。使用 Win32 API 来获取该对话框(如果您只针对 Windows)比使用 Qt 本身找到一种方法更容易:)
  • 是的,在我看来也是如此。所以你认为 Calibre 正在使用#ifdef 和特定于平台的代码?

标签: qt qfiledialog


【解决方案1】:

试试这个:QFileDialog::getExistingDirectory()

两种静态方法都以目录选择模式打开文件选择器。如果您不喜欢打开对话框的外观,则需要实现自己的。默认情况下,如果可能,Qt 会尝试打开原生的,这应该适用于 99.9% 的情况。

【讨论】:

  • 你没有看我的帖子,对吧?我写道我试过这个,但它没有打开正确的对话框。
  • 你可能是对的。写代码的时间过长损害了我的阅读能力。 :)
  • 你能粘贴确切的代码 sn-p 并给我们明显错误对话框的屏幕截图吗?
  • 你在这里:QString dirStr = QFileDialog::getExistingDirectory(this, titlePlain(), pathFromSettings().absolutePath()); 我将图片添加到原始帖子中。该对话框的问题是它没有显示文件,尽管它应该是:stackoverflow.com/questions/19108339/…
  • 我相信这就是它的工作原理 - Qt 调用本机文件选择对话框,过滤掉普通文件。
【解决方案2】:

看来我得自己回答了。 最后,我完全是在 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多