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