Some time ago, a developer asked me if I could use qtquick.dialogs in my Ubuntu phone to implement FileDialog. At present, there is no QT this library on the mobile phone implementation. The main reason is that it does not use the unit grid layout, so it appears very small on the real phone. So how can we achieve the same function?
Let's look at the API Dialog provided by our Ubuntu Qt first. Here we have a control that provides a dialog. We can follow the example above to write the routines we need. In addition, we also need to use another apifolderlistmodel. Through this API we can get all the files we need for the directory of files:
By 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"/* Th Is 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 (height:units.gu) page {title:i18n.tr ("simple") button {id:launcher text:i18n.tr ("Open") Width:units.gu (+) onclicked:p Opuputils.open (dialog, NULL)} Component {Id:dialog dialog {Id:diAlogue title: "FileList Dialog" text: "Show the Files" ListView { Id:listview width:parent.width height:200 Folderlistmo del {Id:foldermodel namefilters: ["*.QML"] } Component {id:filedelegate Text { Id:text Text:filename Mousearea { Anchors.fill:parent onclicked: {Conso Le.log ("It is clicked"); Listview.currentindex = index; }}}}//Define a highlight with Customized Movement BetWeen items. Component {id:highlightbar Rectangle {width:f Iledelegate.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:pa Rent.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)}}}}}}
To run our application, we can see:
It must be noted here that Folderlistmodel does not have access to any directory of the system. We can refer to the article "Ubuntu OS Application Runtime enviroment" to get what directories are accessible. For accessing files owned by another application, we can access them by using Contenthub.
Source code for all applications in: Git clone https://gitcafe.com/ubuntu/filedialog.git
How to implement a filedialog on an Ubuntu phone