简体   繁体   English

Qt中是否可以使用本机“文件浏览器”让用户选择特定文件的路径?

[英]Is there a native “file explorer” in Qt that I can use to have the user select path to a specific file?

I have a small application that requires the pathway to a specific file that will be given at run-time by the user. 我有一个小型应用程序,它需要通向用户在运行时给出的特定文件的途径。 All I need is the path. 我所需要的只是道路。 I image this to be some form of a file explorer in which the user may traverse through the directory tree. 我将其想象成某种形式的文件浏览器 ,用户可以在其中浏览目录树。

Is there a way to do this in Qt, or must I call the native OS implementation (if that is possible). 有没有办法在Qt中做到这一点,或者我必须调用本机OS实现(如果可能的话)。 If not in Qt, how can I make use of the local OS implementation? 如果不在Qt中,如何使用本地OS实现?

This is what QFileDialog is trying to achieve, so I would suggest to use that if it is a widget based application. 这就是QFileDialog试图达到的目的,所以如果它是基于窗口小部件的应用程序,我建议使用它。 All you will need to write is something like this: 您只需要编写如下内容:

fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

This will bring up a dialog for selection that the user can use for navigation. 这将弹出一个供用户选择的对话框,用户可以使用该对话框进行导航。

If you happen to use QML, you could give a try to the FileDialog component . 如果碰巧使用QML,则可以尝试使用FileDialog组件 Then, you would write something like this: 然后,您将编写如下内容:

import QtQuick 2.2
import QtQuick.Dialogs 1.0

FileDialog {
    id: fileDialog
    title: "Please choose a file"
    onAccepted: {
        console.log("You chose: " + fileDialog.fileUrls)
        Qt.quit()
    }
    onRejected: {
        console.log("Canceled")
        Qt.quit()
    }
    Component.onCompleted: visible = true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM