How to implement a FileDialog and ubuntufiledialog on Ubuntu mobile phones
Some time ago, a developer asked me if I could use QtQuick. Dialogs in Ubuntu to implement FileDialog. Currently, the Qt library is not implemented on mobile phones. The main reason is that it does not use the unit grid layout, so it looks very small on a real mobile phone. So how can we achieve the same function?
First, let's take a look at the API Dialog provided by our Ubuntu Qt. Here we provide a Dialog control. We can write the routines we need following the examples above. In addition, we also need to use another API FolderListModel. Through this API, we can obtain all the files in the directory of the files we need:
Through understanding and using these two APIs, we can quickly make the following test applications:
import QtQuick 2.0import Ubuntu.Components 1.1import Ubuntu.Components.Popups 0.1import Qt.labs.folderlistmodel 2.1/*! \brief MainView with a Label and Button elements.*/MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "filedialog.ubuntu" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false width: units.gu(60) height: units.gu(75) Page { title: i18n.tr("Simple") Button { id: launcher text: i18n.tr("Open") width: units.gu(16) onClicked: PopupUtils.open(dialog, null) } Component { id: dialog Dialog { id: dialogue title: "FileList Dialog" text: "Show the files" ListView { id: listview width: parent.width height: 200 FolderListModel { id: folderModel nameFilters: ["*.qml"] } Component { id: fileDelegate Text { id: text text: fileName MouseArea { anchors.fill: parent onClicked: { console.log("it is clicked"); listview.currentIndex = index; } } } } // Define a highlight with customized movement between items. Component { id: highlightBar Rectangle { width: fileDelegate.width; height: 50 color: "red" y: listview.currentItem.y; Behavior on y { SpringAnimation { spring: 2; damping: 0.1 } } } } focus: true model: folderModel delegate: fileDelegate highlight: highlightBar } Row { id: row width: parent.width spacing: units.gu(1) Button { width: parent.width/2 text: "Cancel" onClicked: PopupUtils.close(dialogue) } Button { width: parent.width/2 text: "Confirm" color: UbuntuColors.green onClicked: { console.log("caller: " + dialogue.caller); console.log("currentIndex: " + listview.currentIndex); console.log(folderModel.get(listview.currentIndex, "fileName")); launcher.update(); PopupUtils.close(dialogue) } } } } } }}
Run our application. We can see that:
It must be noted that FolderListModel is not accessible to any directory of the system. We can refer to the article "Ubuntu OS application Runtime Enviroment" to find out which directories are accessible. You can use contenthub to access files owned by another application.
Source code for all applications in: git clone https://gitcafe.com/ubuntu/filedialog.git