Development/Tutorials/Plasma4/JavaScript/API-FileDialog: Difference between revisions

From KDE TechBase
(Adding an example for FileDialog extension.)
 
Line 23: Line 23:
** '''accepted(FileDialogProxy)'''': emitted when the file dialog has been successfully accepted by the user with one or more files/directories.
** '''accepted(FileDialogProxy)'''': emitted when the file dialog has been successfully accepted by the user with one or more files/directories.
** '''finished(FileDialogProxy)''': emitted when the file dialog closes, included when cancelled/closed without being accepted
** '''finished(FileDialogProxy)''': emitted when the file dialog closes, included when cancelled/closed without being accepted
= Examples =
This example create a new OpenFileDialog instance, so the user can select a file. If a file has been selected and the "Accept" button is clicked then it will call the "loadFile" function.
<syntaxhighlight lang="javascript" line>
function loadFile(fileDialogProxy){
  // Here we load the file. We retrieve the user's choice using fileDialogProxy.file
 
  // fileDialogProxy use the same methods as FileDialog shown before.
}
function selectFile() {
    var fd = new OpenFileDialog();
    fd.accepted.connect(loadFile);
    fd.show();
}
</syntaxhighlight>
Remember that signals are connected using the '''connect(...)''' method as explained at [[Development/Tutorials/Plasma/JavaScript/GettingStarted#QtScript|Plasma using Javascript: Getting Started]]

Latest revision as of 19:55, 22 February 2015

FileDialog

This Extension provides access to open and save dialog classes: OpenFileDialog and SaveFileDialog. Both are non-modal and run asynchronously, so the signals must be used. Other than the name difference (and resulting UI variance) the API for each is identical:

  • Constructors
    • OpenFileDialog
    • SaveFileDialog
  • Properties
    • Read Only
      • array(Url) urls: the selected file, as a Url object
      • Url baseUrl, the current path (minus filename) as a Url
      • string file: the selected file, as a string
      • array(string) files: selected files (plural), as an array of strings
    • Read/Write
      • Url url: the current Url, can be read from when the user is done or assigned before to set the starting path
      • string filter: a string representing the mimetype filter; e.g. "*.cpp|C++ Source Files\n*.h|Header files" or "*.cpp" or "*.cpp|*h"
      • boolean localOnly: true to show only local files, false if network locations are Ok as well
      • boolean directoriesOnly: true to only allow selection of a directory (not a file)
      • boolean existingOnly: true if only existing files/directories may be selected
  • Functions
    • show(): when called, the dialog will be shown to the user
  • Signals
    • accepted(FileDialogProxy)': emitted when the file dialog has been successfully accepted by the user with one or more files/directories.
    • finished(FileDialogProxy): emitted when the file dialog closes, included when cancelled/closed without being accepted

Examples

This example create a new OpenFileDialog instance, so the user can select a file. If a file has been selected and the "Accept" button is clicked then it will call the "loadFile" function.

function loadFile(fileDialogProxy){
  // Here we load the file. We retrieve the user's choice using fileDialogProxy.file
  
  // fileDialogProxy use the same methods as FileDialog shown before.
}

function selectFile() {
    var fd = new OpenFileDialog();
    fd.accepted.connect(loadFile);

    fd.show();
}

Remember that signals are connected using the connect(...) method as explained at Plasma using Javascript: Getting Started