QML FileDialog

I am a user coming from Matlab and I missed the Matlab functionality of uigetfile. When I’m prototyping functions that process data it’s handy to have a simple function that opens a selection dialog and returns the filename. I use it so that I don’t have to manually specify the filestring when I mucking around with different files and such. I played around for awhile and got the below implementation working using QML.jl based primarily on the filedialog example they provide. I like that the dialog is native and so looks okay on different systems.

This worked great to the point that I made the little module below and put it in my juliarc.jl file so I would always have it and was pleased for the last month using it. Then today it stopped working. I can’t seem to figure out why it just hangs when called. As far as I can tell QML.jl has been updated and nothing else has changed. I did have to reinstall QML after installing an entirely different package went awry causing Julia to fail to start the REPL, leading me to remove all my packages and reinstall them. The QML examples work and the filedialog example that this was based on hasn’t changed.

Does anyone have ideas on how to make this work, or other ways to have similar functionality that is relatively simple? It needs to be low on dependencies (QML.jl takes care of all the install by itself which counts as low) and native on different systems.

module FileDialog
using QML

function filedialog(;title::String="File Dialog",foldermode::Bool=false,multiselect::Bool=false,filter::Array{String}=["All files (*)"],folder::String=pwd(),savemode::Bool=false)

qml_data = QByteArray("""
import QtQuick 2.2
import QtQuick.Dialogs 1.0
import QtQuick.Controls 1.0
import org.julialang 1.0

ApplicationWindow {
  title: "FileDialog"
  width: 640
  height: 480
  visible: false

  FileDialog {
      id: fileDialog
      title: Title
      selectMultiple: MultiSelect
      selectFolder: FolderSelect
      selectExisting: SelectExisting
      nameFilters: Filter
      folder: Folder
      onAccepted: {

          Julia.getfilelist(fileDialog.fileUrls)
          Qt.quit()
      }
      onRejected: {
          console.log("Canceled")
          Qt.quit()
      }
      Component.onCompleted: visible = true
  }
}
""")


filelist=AbstractArray{}

function getfilelist(uri_list)
  filelist=uri_list
end

@qmlfunction getfilelist

qengine = init_qmlengine()
qcomp = QQmlComponent(qengine)
set_data(qcomp, qml_data, "")
set_context_property(qmlcontext(),"Title",title)
set_context_property(qmlcontext(),"FolderSelect",foldermode)
set_context_property(qmlcontext(),"MultiSelect",multiselect)
set_context_property(qmlcontext(),"Filter",filter)
set_context_property(qmlcontext(),"Folder",folder)
set_context_property(qmlcontext(),"SelectExisting",!savemode)
create(qcomp, qmlcontext());


exec()

return filelist
end

function uigetfile(;title::String="Select a File",multiselect::Bool=false,filter::Array{String}=["All files (*)"],folder::String=pwd())
file=filedialog(;title=title,foldermode=false,multiselect=multiselect,filter=filter,folder=folder,savemode=false)
end

function uigetdir(;title::String="Select a Folder",folder::String=pwd())
file=filedialog(;title=title,foldermode=true,multiselect=false,folder=folder,savemode=false)
return file
end

function uisavefile(;title::String="Save File As",filter::Array{String}=["All files (*)"],folder::String=pwd())
file=filedialog(;title=title,foldermode=false,multiselect=false,filter=filter,folder=folder,savemode=true)
return file
end

export uigetfile, uigetdir, uisavefile

end

Have you tried running this example ? The corresponding QML file is in the QML folder.

Otherwise, Gtk also has a file dialog: see here

Yeah, I ran all the QML tests and it ran without issue.

I used that one at first. If nothing else is does work. It doesn’t use the native dialog but it’s atleast something. (Maybe sometime I’ll figure out how to make GTK.jl use the gtk native dialog chooser.

@Jordan_Cluts

Your module works for me (after some reformating, and moving the export command to to top, but not sure it’s needed).

julia> include(“FileDialog.jl”)
=> FileDialog
julia> using FileDialog
julia> uigetfile(;folder=“…”)
=> I could change directory then choose a file and finally got the String array of its path.

– Maurice

1 Like

Thank you so much for giving it a run. I’m always amazed at how helpful this community is. After hearing that it worked for someone else I finally tried what I should’ve done in the first place and rebooted my computer. I still don’t know what the issue was but it works now. So thanks for the confirmation that my code wasn’t the problem and I should look elsewhere.