Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
filedialog.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webgui
3///
4/// Example showing how RFileDialog can be used in sync and async modes.
5/// \macro_code
6///
7/// \date 2019-11-01
8/// \author Sergey Linev <S.Linev@gsi.de>
9
10/*************************************************************************
11 * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers. *
12 * All rights reserved. *
13 * *
14 * For the licensing terms see $ROOTSYS/LICENSE. *
15 * For the list of contributors see $ROOTSYS/README/CREDITS. *
16 *************************************************************************/
17
18// Normally file dialogs will be used inside other widgets as ui5 dialogs.
19// By default, dialog starts in async mode - means macro immediately returns to command line
20// To start OpenFile dialog in sync mode, call `root "filedialog.cxx(1)" -q`.
21// Once file is selected, root execution will be stopped.
22
23// macro must be here to let macro work on Windows
25
26#include <ROOT/RFileDialog.hxx>
27
28void filedialog(int kind = 0)
29{
30 std::string fileName;
31
32 // example of sync methods, blocks until name is selected
33 switch (kind) {
34 case 1: fileName = ROOT::RFileDialog::OpenFile("OpenFile title"); break;
35 case 2: fileName = ROOT::RFileDialog::SaveAs("SaveAs title", "newfile.xml"); break;
36 case 3: fileName = ROOT::RFileDialog::NewFile("NewFile title", "test.txt"); break;
37 }
38
39 if (kind > 0) {
40 printf("Selected file: %s\n", fileName.c_str());
41 return;
42 }
43
44 auto dialog = std::make_shared<ROOT::RFileDialog>(ROOT::RFileDialog::kOpenFile, "OpenFile dialog in async mode");
45
46 dialog->SetNameFilters({"C++ files (*.cxx *.cpp *.c *.C)", "ROOT files (*.root)", "Image files (*.png *.jpg *.jpeg)",
47 "Text files (*.txt)", "Any files (*)"});
48
49 dialog->SetSelectedFilter("ROOT files");
50
51 // use dialog capture to keep reference until file name is selected
52 dialog->SetCallback([dialog](const std::string &res) mutable {
53 printf("Selected file: %s\n", res.c_str());
54
55 // cleanup dialog - actually not needed, lambda is cleaned up after that call anyway
56 // dialog.reset();
57 });
58
59 dialog->Show();
60}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:474
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
static std::string SaveAs(const std::string &title="", const std::string &fname="")
Start SaveAs dialog.
static std::string OpenFile(const std::string &title="", const std::string &fname="")
Start OpenFile dialog.
static std::string NewFile(const std::string &title="", const std::string &fname="")
Start NewFile dialog.