如何使用QML FileDialog选择多个文件和目录

时间:2015-11-12 12:24:38

标签: qt qml

我开始使用Qt 5.5 我想尽可能多地使用QML来处理UI问题 我需要一个文件和目录选择器,可以选择多个对象,文件或目录 QFileDialog能够这样做,根据this solutionQFileDialog需要设置为非原生。
根据{{​​3}},程序员无法强制QML FileDialog对象为非本地对象。

我必须使用C ++来处理这个问题,或者是否有办法访问QML FileDialog的属性以强制实现基础QFileDialog的实例化,然后是一种访问此方法的方法来自QML的实例属性,以便在QML中实现解决方案?

1 个答案:

答案 0 :(得分:1)

编辑#1:请参阅以下评论。 @SR_是对的。我认为有必要在C ++中实现一些代码以获得预期的行为。

编辑#2:我之前的回答是错误的。将selectMultiple属性设置为true不是解决方案。所以我更新了答案以显示新提案。有关此问题的讨论的详细信息,请参阅下面的评论。

下一个代码基于Qt示例File System Browser,代码为uploaded到GitHub。

<强>的main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QFileSystemModel>
#include "filemanagement.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    FileManagement fileManagement;
    QQmlApplicationEngine engine;
    QFileSystemModel *fsm = new QFileSystemModel(&engine);
    fsm->setRootPath(QDir::homePath());
    fsm->setResolveSymlinks(true);
    engine.rootContext()->setContextProperty("fileManagement", &fileManagement);
    engine.rootContext()->setContextProperty("fileSystemModel", fsm);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

    return app.exec();
}

<强> main.qml

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQml.Models 2.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("File System")

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    Row {
        id: row
        anchors.top: parent.top
        anchors.topMargin: 12
        anchors.horizontalCenter: parent.horizontalCenter

        ExclusiveGroup {
            id: eg
        }

        Repeater {
            model: [ "None", "Single", "Extended", "Multi", "Contig."]
            Button {
                text: modelData
                exclusiveGroup: eg
                checkable: true
                checked: index === 1
                onClicked: view.selectionMode = index
            }
        }
    }

    ItemSelectionModel {
        id: sel
        model: fileSystemModel
        onSelectionChanged: {
            console.log("selected", selected)
            console.log("deselected", deselected)
            fileManagement.printFileNames(model, selectedIndexes)
        }
        onCurrentChanged: console.log("current", current)
    }

    TreeView {
        id: view
        anchors.fill: parent
        anchors.margins: 2 * 12 + row.height
        model: fileSystemModel
        selection: sel

        onCurrentIndexChanged: console.log("current index", currentIndex)

        TableViewColumn {
            title: "Name"
            role: "fileName"
            resizable: true
        }

        TableViewColumn {
            title: "Permissions"
            role: "filePermissions"
            resizable: true
        }

        onClicked: {
            console.log("clicked", index)            
            fileManagement.printPath(index.model, index)
        }
        onDoubleClicked: isExpanded(index) ? collapse(index) : expand(index)
    }

    Component.onCompleted: fileManagement.test()
}

<强> filemanagement.h

#ifndef FILEMANAGEMENT_H
#define FILEMANAGEMENT_H

#include <QObject>
#include <QDebug>
#include <QFileSystemModel>

class FileManagement : public QObject
{
    Q_OBJECT
public:
    explicit FileManagement(QObject *parent = 0);

    Q_INVOKABLE bool printPath(QFileSystemModel* fileSystemModel,
                               const QModelIndex &modelIndex);

    Q_INVOKABLE bool printFileNames(QFileSystemModel* fileSystemModel,
                                    QList<QModelIndex> modelIndexList);

signals:

public slots:

    void test() {
        qDebug() << "Called the C++ slot";
    }
};

#endif // FILEMANAGEMENT_H

<强> filemanagement.cpp

#include "filemanagement.h"

FileManagement::FileManagement(QObject *parent) : QObject(parent)
{

}

bool FileManagement::printPath(QFileSystemModel* fileSystemModel,
                       const QModelIndex &modelIndex) {
    qDebug() << "+ File path: " << fileSystemModel->filePath(modelIndex);
    return true;
}

bool FileManagement::printFileNames(QFileSystemModel* fileSystemModel,
                            QList<QModelIndex> modelIndexList) {
    qDebug() << "+ " << modelIndexList.size() << " items selected: ";
    QList<QModelIndex>::iterator i;
    for (i = modelIndexList.begin(); i != modelIndexList.end(); ++i) {
        qDebug() << "++ File name: " << fileSystemModel->fileName(*i);
    }

    return true;
}