qml Android linux windows 打开文件选择

9 篇文章 0 订阅

先说废话:
经过一段时间对Qt的学习,想在Android上搞点事情,中途遇到了一个大坑,就是打开Android的文件系统进行文件选择,别以为这个功能简单,直接使用一个什么FileDialog就可以完事了,你要是这样想,兄弟,你把这个页面关了吧。


一、实现功能:

本博客的功能是讲解在Qml在Android平台下进行文件选择,网上的人说了一大堆,缺胳膊少腿的,还是官方给的案列比较靠谱点。


二、运行效果:

在这里插入图片描述
备注,这个是在我安卓手机上横屏运行的,在其他平台也应该是这个样子的,望周知。


三、开发环境:

1、Qt5.12,应该Qt5.9之后的平台都是支持的,你可以自己试试,我比较懒的。
2、Android运行版本是8.0, 6.0之后的版本需要进行权限申请,这个骚操作会在后面描述的。
3、还需要你在你的工程中,配置一个图标字体,FontAwsome,这样看起来才舒服,不然就很阔怕的丑。如果你不知道咋个配置,请看这里,照着做就可以了。


四、代码编写:

4.1、首先编写文件选择qml代码,有点多,自己复制粘贴就好了,这个文件命名为: FileBrowser.qml:

/*
 * Note:this file base on Qt Company.
 * if you want to use this file, you
 * need configure font fontawesome-webfont.ttf
 * in your project and some icon will be show normal.
 *
*/

import QtQuick 2.0
import Qt.labs.folderlistmodel 2.0

Rectangle {
    id: fileBrowser
    color: "transparent"

    // 当前文件夹路径
    property string folder
    // 选择文件信号
    signal fileSelected(string file)
    // 文件图标显示字体库
    property string iconfamily: "FontAwsome"

    property int itemHeight: Math.min(parent.width, parent.height) / 15
    property int buttonHeight: Math.min(parent.width, parent.height) / 12

    function selectFile(file) {
        if (file !== "") {
            folder = loader.item.folders.folder
            fileBrowser.fileSelected(file)
        }
        loader.sourceComponent = undefined
    }

    Loader {
        id: loader
    }

    function show() {
        loader.sourceComponent = fileBrowserComponent
        loader.item.parent = fileBrowser
        loader.item.anchors.fill = fileBrowser
        loader.item.folder = fileBrowser.folder
    }

    Component {
        id: fileBrowserComponent

        Rectangle {
            id: root
            color: "black"
            property bool showFocusHighlight: false
            property variant folders: folders1
            property variant view: view1
            property alias folder: folders1.folder
            property color textColor: "white"

            FolderListModel {
                id: folders1
                folder: folder
            }

            FolderListModel {
                id: folders2
                folder: folder
            }

            SystemPalette {
                id: palette
            }

            Component {
                id: folderDelegate

                Rectangle {
                    id: wrapper
                    function launch() {
                        var path = "file://";
                        if (filePath.length > 2 && filePath[1] === ':') // Windows drive logic, see QUrl::fromLocalFile()
                            path += '/';
                        path += filePath;
                        if (folders.isFolder(index))
                            down(path);
                        else
                            fileBrowser.selectFile(path)
                    }
                    width: root.width
                    height: folderImage.height
                    color: "transparent"

                    Rectangle {
                        id: highlight
                        visible: false
                        anchors.fill: parent
                        anchors.leftMargin: 5
                        anchors.rightMargin: 5
                        color: "#212121"
                    }

                    Item {
                        id: folderImage
                        width: itemHeight
                        height: itemHeight
                        Image {
                            id: folderPicture
                            width: itemHeight * 0.9
                            height: itemHeight * 0.9
                            anchors.left: parent.left
                            anchors.margins: 5
                            visible: folders.isFolder(index)
                            Text {
                                anchors.fill: parent
                                verticalAlignment: Text.AlignVCenter
                                horizontalAlignment: Text.AlignHCenter
                                font.family: iconfamily
                                text: "\uf115"
                                color: "white"
                                Component.onCompleted: font.pointSize = parent.width / 2
                            }
                        }
                        Image {
                            width: itemHeight * 0.9
                            height: itemHeight * 0.9
                            anchors.left: parent.left
                            anchors.margins: 5
                            visible: !folders.isFolder(index)
                            Text {
                                anchors.fill: parent
                                verticalAlignment: Text.AlignVCenter
                                horizontalAlignment: Text.AlignHCenter
                                font.family: iconfamily
                                text: "\uf0f6"
                                color: "white"
                                Component.onCompleted: font.pointSize = parent.width / 2
                            }
                        }
                    }

                    Text {
                        id: nameText
                        anchors.fill: parent;
                        verticalAlignment: Text.AlignVCenter
                        text: fileName
                        anchors.leftMargin: itemHeight + 10
                        color: (wrapper.ListView.isCurrentItem && root.showFocusHighlight) ? palette.highlightedText : textColor
                        elide: Text.ElideRight
                    }

                    MouseArea {
                        id: mouseRegion
                        anchors.fill: parent
                        onPressed: {
                            root.showFocusHighlight = false;
                            wrapper.ListView.view.currentIndex = index;
                        }
                        onClicked: { if (folders === wrapper.ListView.view.model) launch() }
                    }

                    states: [
                        State {
                            name: "pressed"
                            when: mouseRegion.pressed
                            PropertyChanges { target: highlight; visible: true }
                            PropertyChanges { target: nameText; color: palette.highlightedText }
                        }
                    ]
                }
            }

            ListView {
                id: view1
                anchors.top: titleBar.bottom
                anchors.bottom: cancelButton.top
                width: parent.width
                model: folders1
                delegate: folderDelegate
                highlight: Rectangle {
                    color: "#212121"
                    visible: root.showFocusHighlight && view1.count != 0
                    width: view1.currentItem == null ? 0 : view1.currentItem.width
                }
                highlightMoveVelocity: 1000
                pressDelay: 100
                focus: true
                state: "current"
                states: [
                    State {
                        name: "current"
                        PropertyChanges { target: view1; x: 0 }
                    },
                    State {
                        name: "exitLeft"
                        PropertyChanges { target: view1; x: -root.width }
                    },
                    State {
                        name: "exitRight"
                        PropertyChanges { target: view1; x: root.width }
                    }
                ]
                transitions: [
                    Transition {
                        to: "current"
                        SequentialAnimation {
                            NumberAnimation { properties: "x"; duration: 250 }
                        }
                    },
                    Transition {
                        NumberAnimation { properties: "x"; duration: 250 }
                        NumberAnimation { properties: "x"; duration: 250 }
                    }
                ]
                Keys.onPressed: root.keyPressed(event.key)
            }

            ListView {
                id: view2
                anchors.top: titleBar.bottom
                anchors.bottom: parent.bottom
                x: parent.width
                width: parent.width
                model: folders2
                delegate: folderDelegate
                highlight: Rectangle {
                    color: "#212121"
                    visible: root.showFocusHighlight && view2.count != 0
                    width: view1.currentItem == null ? 0 : view1.currentItem.width
                }
                highlightMoveVelocity: 1000
                pressDelay: 100
                states: [
                    State {
                        name: "current"
                        PropertyChanges { target: view2; x: 0 }
                    },
                    State {
                        name: "exitLeft"
                        PropertyChanges { target: view2; x: -root.width }
                    },
                    State {
                        name: "exitRight"
                        PropertyChanges { target: view2; x: root.width }
                    }
                ]
                transitions: [
                    Transition {
                        to: "current"
                        SequentialAnimation {
                            NumberAnimation { properties: "x"; duration: 250 }
                        }
                    },
                    Transition {
                        NumberAnimation { properties: "x"; duration: 250 }
                    }
                ]
                Keys.onPressed: root.keyPressed(event.key)
            }

            Rectangle {
                width: parent.width
                height: buttonHeight + 10
                anchors.bottom: parent.bottom
                color: "black"
            }

            Rectangle {
                id: cancelButton
                width: parent.width
                height: buttonHeight
                color: "#212121"
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.margins: 5
                radius: buttonHeight / 15

                Text {
                    anchors.fill: parent
                    text: "Cancel"
                    color: "white"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: fileBrowser.selectFile("")
                }
            }

            Keys.onPressed: {
                root.keyPressed(event.key);
                if (event.key === Qt.Key_Return || event.key === Qt.Key_Select || event.key === Qt.Key_Right) {
                    view.currentItem.launch();
                    event.accepted = true;
                } else if (event.key === Qt.Key_Left) {
                    up();
                }
            }


            Rectangle {
                id: titleBar
                width: parent.width
                height: buttonHeight + 10
                anchors.top: parent.top
                color: "black"

                Rectangle {
                    width: parent.width;
                    height: buttonHeight
                    color: "#212121"
                    anchors.margins: 5
                    anchors.top: parent.top
                    anchors.left: parent.left
                    anchors.right: parent.right
                    radius: buttonHeight / 15

                    Rectangle {
                        id: upButton
                        width: buttonHeight
                        height: buttonHeight
                        color: "transparent"
                        Image {
                            width: itemHeight
                            height: itemHeight
                            anchors.centerIn: parent
                            Text {
                                anchors.fill: parent
                                verticalAlignment: Text.AlignVCenter
                                horizontalAlignment: Text.AlignHCenter
                                font.family: iconfamily
                                text: "\uf112"
                                color: "white"
                                Component.onCompleted: font.pointSize = parent.width / 2
                            }
                        }
                        MouseArea { id: upRegion; anchors.centerIn: parent
                            width: buttonHeight
                            height: buttonHeight
                            onClicked: up()
                        }
                        states: [
                            State {
                                name: "pressed"
                                when: upRegion.pressed
                                PropertyChanges { target: upButton; color: palette.highlight }
                            }
                        ]
                    }

                    Text {
                        anchors.left: upButton.right; anchors.right: parent.right; height: parent.height
                        anchors.leftMargin: 5; anchors.rightMargin: 5
                        text: folders.folder
                        color: "white"
                        elide: Text.ElideLeft;
                        horizontalAlignment: Text.AlignLeft;
                        verticalAlignment: Text.AlignVCenter
                    }
                }
            }

            function down(path) {
                if (folders == folders1) {
                    view = view2
                    folders = folders2;
                    view1.state = "exitLeft";
                } else {
                    view = view1
                    folders = folders1;
                    view2.state = "exitLeft";
                }
                view.x = root.width;
                view.state = "current";
                view.focus = true;
                folders.folder = path;
            }

            function up() {
                var path = folders.parentFolder;
                if (path.toString().length === 0 || path.toString() === 'file:')
                    return;
                if (folders == folders1) {
                    view = view2
                    folders = folders2;
                    view1.state = "exitRight";
                } else {
                    view = view1
                    folders = folders1;
                    view2.state = "exitRight";
                }
                view.x = -root.width;
                view.state = "current";
                view.focus = true;
                folders.folder = path;
            }

            function keyPressed(key) {
                switch (key) {
                case Qt.Key_Up:
                case Qt.Key_Down:
                case Qt.Key_Left:
                case Qt.Key_Right:
                    root.showFocusHighlight = true;
                    break;
                default:
                    // do nothing
                    break;
                }
            }
        }
    }
}

4.2、配置Android平台权限:

1)在你的pro工程文件中添加如下代码:

android{
QT     += androidextras
DEFINES += Android_Platform
}

2)在你的main.cpp中添加如下代码:

#include <QtCore/QStandardPaths>

#ifdef Android_Platform
#include <QtAndroid>
#endif

int  main(){

#ifdef Android_Platform
    // 添加读取权限,Android 6.0 版本后需要申请,不然的话,啥都看不到。
    QString strPermission = "android.permission.READ_EXTERNAL_STORAGE";
    QtAndroid::requestPermissionsSync(QStringList() << strPermission);  // ok
#endif

QQmlApplicationEngine engine;
// 获取默认文件的路径并进行设置
    const QStringList moviesLocation = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
    const QUrl videoPath =
            QUrl::fromLocalFile(moviesLocation.isEmpty() ?
                                    app.applicationDirPath() :
                                    moviesLocation.front());
    engine.rootContext()->setContextProperty("videoPath", videoPath);
}

3)经过上诉的配置后,即可在qml中进行使用了:
a、实例化对象:

    FileBrowser{
        id: filebrowser
        anchors.fill: parent
        folder: videoPath
        onFileSelected:{
            console.log(file)
        }
    }

b、打开文件选择框:

onClicked: {
           filebrowser.show()
}

OK,到这里就已经完美了,请仔细看我的代码,我可以,你也可以的。
有问题可以邮件我。
543985125@qq.com

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现最近打开功能,我们需要记录用户打开的文件名和路径,并将其保存到文件中。下次启动应用程序时,我们可以从文件中读取最近打开的文件列表,并将其显示在应用程序的 UI 中。以下是一个简单的 QML 实现: ``` import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 import Qt.labs.settings 1.0 ApplicationWindow { id: mainWindow width: 640 height: 480 visible: true title: "QML记事本" // 设置对象 Settings { id: settings category: "RecentFiles" property int count: 0 // 添加最近打开的文件 function addRecentFile(fileUrl) { var index = count; settings.setValue("file_" + index + "_url", fileUrl); settings.setValue("file_" + index + "_name", fileUrl.substring(fileUrl.lastIndexOf("/") + 1)); settings.setValue("file_" + index + "_time", new Date().getTime()); settings.setValue("count", index + 1); } // 获取最近打开的文件列表 function getRecentFiles() { var files = []; for (var i = 0; i < count; i++) { var fileUrl = settings.value("file_" + i + "_url", ""); var fileName = settings.value("file_" + i + "_name", ""); var fileTime = settings.value("file_" + i + "_time", ""); files.push({url: fileUrl, name: fileName, time: fileTime}); } return files; } } // 布局 ColumnLayout { anchors.fill: parent // 文本编辑器 TextArea { id: textEditor Layout.fillWidth: true Layout.fillHeight: true } // 操作栏 RowLayout { Layout.fillWidth: true // 新建按钮 Button { text: "新建" onClicked: { textEditor.text = ""; } } // 打开按钮 Button { text: "打开" onClicked: { var fileDialog = Qt.createQmlObject('import QtQuick.Dialogs 1.2; FileDialog { selectMultiple: false; selectFolder: false }', mainWindow); fileDialog.visible = true; fileDialog.onAccepted: { var fileUrl = fileDialog.fileUrl; if (fileUrl !== "") { // 添加最近打开的文件 settings.addRecentFile(fileUrl); // 读取文件内容并显示 var file = new XMLHttpRequest(); file.open("GET", fileUrl); file.onreadystatechange = function() { if (file.readyState === XMLHttpRequest.DONE) { textEditor.text = file.responseText; } } file.send(); } } } } // 最近打开菜单 Menu { title: "最近打开" MenuItem { text: "清空" onClicked: { settings.clear(); recentFilesMenu.clear(); } } MenuSeparator {} // 最近打开的文件列表 Menu { id: recentFilesMenu title: "文件列表" visible: settings.count > 0 // 获取最近打开的文件列表并添加到菜单中 Component.onCompleted: { var files = settings.getRecentFiles(); for (var i = 0; i < files.length; i++) { var file = files[i]; addAction({text: file.name, triggered: function() { var fileUrl = this.data.url; var file = new XMLHttpRequest(); file.open("GET", fileUrl); file.onreadystatechange = function() { if (file.readyState === XMLHttpRequest.DONE) { textEditor.text = file.responseText; } } file.send(); }, data: file}); } } } } } } } ``` 上述代码中,我们使用了 `Settings` 对象来记录最近打开的文件列表。具体实现如下: - `addRecentFile()` 函数用于添加最近打开的文件,它接受一个文件 URL 参数,并将文件名、路径和最近打开时间保存到设置中。 - `getRecentFiles()` 函数用于获取最近打开的文件列表,它返回一个包含文件 URL、文件名和最近打开时间的对象数组。 - 在打开文件时,我们首先调用 `addRecentFile()` 函数将文件 URL 添加到最近打开的文件列表中,然后读取文件内容并显示在文本编辑器控件中。 - 在最近打开菜单中,我们使用 `Menu` 和 `MenuItem` 控件来显示最近打开的文件列表。在菜单中,我们首先添加了一个清空菜单项,然后使用 `Component.onCompleted` 事件处理程序初始化最近打开的文件列表。在初始化过程中,我们使用 `settings.getRecentFiles()` 函数获取最近打开的文件列表,并将其添加到菜单中。当用户点击菜单项时,我们使用 `XMLHttpRequest` 对象读取文件内容并显示在文本编辑器控件中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值