Qt Quick - FileDialog file dialog

I. Overview

FileDialog provides the functionality of a basic file chooser: it allows the user to select an existing file or directory, or create a new filename.

The dialog is initially invisible. You need to first set the property as desired, then set visible to true or call open().

A file dialog window is automatically temporary with respect to its parent window. So, whether you declare the dialog in the project or in the window, the dialog will be displayed in the center of the window containing the project or the window you declare it in .

The implementation of FileDialog will be a platform file dialog. If that fails, then it tries to instantiate a QFileDialog. If that fails too, then it falls back to the QML implementation, defaultfildialog.QML. In this case, you can customize the appearance by editing this file.

DefaultFileDialog.qml contains a rectangle to hold the content of the dialog, because some embedded systems do not support multiple top-level windows. When the dialog becomes visible, it is automatically wrapped in a window if possible, or simply re-parents on top of the main window if there can only be one.

The QML implementation has a sidebar containing shortcuts to common platform-specific locations, as well as user-modifiable favorites. It uses application-specific settings to store the user's favorites, as well as other user-modifiable state, such as whether to show sidebars, the position of splitters, and the size of dialog boxes. These settings are stored in a section of application-specific QSettings called QQControlsFileDialog.

For example, when testing an application using qml tools, a QQControlsFileDialog section is created in the qml runtime settings file (or registry key). If the application is launched via a custom c++ main() function, it is recommended to set the name, organization and domain so that you can control where the application is set . Sometimes going wrong without settings, if you use settings objects in other parts of the application, they will be stored in other parts of the same file.

//如果调用时有问题,就在c++里写下面的东西
setOrganizationName("自己的组织");
setOrganizationDomain("自己的域名");

QFileDialog stores its settings globally, not per application. Platform-native file dialogs may or may not store settings in various platform-dependent ways.

Two, use

Here's a minimal example that opens a file dialog and exits after the user selects a file:

  import QtQuick 2.2
  import QtQuick.Dialogs 1.0

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

Note that a string url is returned. If you use this string in c++, you need to convert this url to QUrl -> QString

//设 qmlStr 是从qml到c++里面的路径url字符串
QUrl url(qmlStr );
QString localFilePath = url.toLocalFile();

QFile(localFilePath );

//下面就是处理文件之类的

3. Common properties

fileUrl : url

fileUrls : list

  • They are all paths to save files and the like.

defaultSuffix : string

  • This property holds the suffix added to the filename if no other suffix is ​​specified.
    This property specifies a string that will be added to the filename if it has no suffix. The suffix is ​​usually used to indicate the file type (for example: where txt is a text file).
    If the first character is a period ('.'), remove it.

nameFilters : list

  • A list of strings to use as filename filters. Each string can be a space-separated list of filters; filters may include ? and * wildcards. Filter lists can also be enclosed in parentheses and provide a textual description of the filter. Like the one below.
 FileDialog {
    
    
      nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
  }

shortcuts : Object

  • A mapping of some useful paths from QStandardPaths to their urls. When creating a file dialog, each path is verified to exist on the user's computer before being added to the list. For example, shortcuts.home will provide the URL of the user's home directory.
attribute name Qt corresponding value effect
desktop QStandardPaths::DesktopLocation The user's desktop path.
documents QStandardPaths::DocumentsLocation user's document path
home QStandardPaths::HomeLocation user home directory.
music QStandardPaths::MusicLocation User Music Catalog
movies QStandardPaths::MoviesLocation User Media Directory
pictures QStandardPaths::PicturesLocation User's photo directory

4. Common examples

1. Open the text file by single selection

FileDialog {
    
    
            id: fileDialog
            folder: shortcuts.desktop
            nameFilters: [ "文本文件 (*.txt *.mcp)", "All files (*)" ]
            title: "请选择一个文件"
            onAccepted: {
    
    
                backend.filePath = fileUrl
            }
            onRejected: {
    
    
               fileDialog.close();
            }
}

insert image description here

2. Single-select to save the text file

Here is to set selectExisting: false because it is to save the file

FileDialog {
    
    
             id: saveDialog
             title: "另存文件"
             folder: shortcuts.desktop
             nameFilters: [ "文本文件 (*.txt * .mcp)", "All files (*)" ]
            selectExisting: false
             onAccepted: {
    
    
                 console.log(saveDialog.fileUrl)
             }
             onRejected: {
    
    
             }
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/130170675