Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
filedialog.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// \macro_code
5///
6/// \date 2019-11-01
7/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
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// Show how RFileDialog can be used in sync and async modes
19// Normally file dialogs will be used inside other widgets as ui5 dialogs
20// By default, dialog starts in async mode - means macro immediately returns to command line
21// To start OpenFile dialog in sync mode, call `root "filedialog.cxx(1)" -q`.
22// Once file is selected, root execution will be stopped
23
24
25// macro must be here to let macro work on Windows
26R__LOAD_LIBRARY(libROOTBrowserv7)
27
28#include <ROOT/RFileDialog.hxx>
29
30
31void filedialog(int kind = 0)
32{
33 std::string fileName;
34
35 // example of sync methods, blocks until name is selected
36 switch (kind) {
37 case 1: fileName = ROOT::RFileDialog::OpenFile("OpenFile title"); break;
38 case 2: fileName = ROOT::RFileDialog::SaveAs("SaveAs title", "newfile.xml"); break;
39 case 3: fileName = ROOT::RFileDialog::NewFile("NewFile title", "test.txt"); break;
40 }
41
42 if (kind > 0) {
43 printf("Selected file: %s\n", fileName.c_str());
44 return;
45 }
46
47 auto dialog = std::make_shared<ROOT::RFileDialog>(ROOT::RFileDialog::kOpenFile, "OpenFile dialog in async mode");
48
49 dialog->SetNameFilters({ "C++ files (*.cxx *.cpp *.c *.C)", "ROOT files (*.root)", "Image files (*.png *.jpg *.jpeg)", "Text files (*.txt)", "Any files (*)" });
50
51 dialog->SetSelectedFilter("ROOT files");
52
53 // use dialog capture to keep reference until file name is selected
54 dialog->SetCallback([dialog](const std::string &res) mutable {
55 printf("Selected file: %s\n", res.c_str());
56
57 // cleanup dialog - actually not needed, lambda is cleaned up after that call anyway
58 // dialog.reset();
59 });
60
61 dialog->Show();
62}
63
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:491
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.