How to add file browser to gui


I was wondering if anyone could help me get started in adding a file browser (such as is on the left of the TBrowser) to my GUI. I didn’t see a tutorial for this, and I’m not exactly sure how to start. I attempted to look through the TBrowser code but was not able to figure out a starting point. Any assistance is greatly appreciated.



Here is a simple example:

[code]#include “TSystem.h”
#include “TGFrame.h”
#include “TGFileBrowser.h”

void test_browser()
TGMainFrame *pMyMainFrame = new TGMainFrame(gClient->GetDefaultRoot(), 300, 600);
TGFileBrowser *pBrowser = new TGFileBrowser(pMyMainFrame);
pBrowser->AddFSDirectory("/", “/”);
And simply replace the TGMainFrame by your composite frame…

Cheers, Bertrand.

Hi Bertrand,

Thank you so much for that example. It was very helpful, and I was able to modify it such that it appears in my GUI.

One more question about TGFileBrowser: I saw that the documentation does not list any signal functions. However there are Clicked and DoubleClicked functions. I’m assuming these be used in some manner to connect selected files with an action?

Thanks again,

Hello again,

I’m sorry, but I’m still trying to work through and figure out how to work with TGFileBrowser. I was wondering if anyone can give any pointers. I have the TGFileBrowser show up in my GUI. What I would like to do is select a root file from the browser and open it just as if I opened it from a file dialogue window.

When I double click on the root file in TGFileBrowser, the only thing that happens is I get the following message in the ROOT session window: <class TFile*>0x4da2b68
The root file does not expand like it does in TBrowser, but that’s okay because I don’t need it to do that. However, I would like to somehow grab the name of the selected ROOT file so that I can open it in the same manner as when I go through the File open dialogue window. I was thinking of somehow accessing the TGListTree, but none of the methods for TGFileBrowser return that as far as I can tell.

Any suggestions are appreciated. Thank you.


What about [url= post[/url]?
Especially the part about embedding a pair of TGListView/TGFileContainer:

fFileView = new TGListView(fC2,600,200); fFileCont = new TGFileContainer(fFileView->GetViewPort(),100,102, kVerticalFrame,GetWhitePixel()); fFileView->GetViewPort()->SetBackgroundColor(GetWhitePixel()); fFileView->SetContainer(fFileCont); fFileCont->SetFilter("*.root"); fFileCont->ChangeDirectory("data"); fFileView->SetViewMode(kLVDetails); fFileCont->Sort(kSortByName); fFileCont->GetListView()->ResizeColumns(); fC2->AddFrame(fFileView, new TGLayoutHints(kLHintsLeft|kLHintsTop,5,2,2,2)); Where FC2 is a TGCompositeFrame

Cheers, Bertrand.

Hi Bertrand,

Thank you for that post. I think it would work; however, I would like to still be able to browse my local directory. I was looking at the documentation and have been trying to find a TRootBrowser::AddFSDirectory() equivalent. Do I need to manually set it up? Right now, the only files that display are the ones in the current working directory since that is what I set it as, but there is no way to navigate outside of that directory.

If anything is unclear, please let me know. Thank you


Try for example $(ROOTSYS)/tutorials/gui/guitest.C, and select the menu Test->File List…

Cheers, Bertrand.

Hi Bertrand,

Thank you for pointing that out. I think I have something that works now. Thank you so much.

I re open this topic. I would like to use a TGFileBrowser to browse root files, and when clicking on it, have access to the root objects which are inside to plot them on a canvas like in the classical TBrowser. But as explained before, clicking on a root file on the TGFileBrowser only write in the prompt : <class TFile*>0x4da2b68
Is there a way to have the same behavior than in the Browser, in such a way that I can also browse the objects of my root files and plot them using the plot draw option of the TGFileBrowser ?

Thanks in advance


Hi Jérémie,

Browsing the content of the ROOT files is a property of the TBrowser (i.e. the Browse() methods of the ROOT files and ROOT objects), and not a property of the TGFileBrowser…
BTW, you can maybe also take a look at the TestFileList example in the $(ROOTSYS)/tutorials/gui/guitest.C tutorial

Cheers, Bertrand.

Thanks for your help Bertrand. Finally I think I will use the standard TBrowser. Is there a way when launching it to remove the right panel, or at least to hide him like if I was moving the VerticalSlider to the right ?

The only solution I see is to specify only the F option in your .rootrc file

# Browser Options (plugins)
# F: File browser E: Text Editor H: HTML browser
# C: Canvas I: I/O redirection P: Proof G: GL viewer
Browser.Options:             F

But then you’ll have some empty spaces…


Still me and my problems. This solution seems not good to me. The solution I found is to combine a TGFileBrowser to have access easily to all the files tree and a TGListView to have access to the elements in the root files. But I did not succeed to connect the result of the DoubleClick on a file of the TGFileBrowser to the TGListView.

What I tried is to do:

TGFileBrowser *pBrowser = new TGFileBrowser(fMain);
fMain->AddFrame(pBrowser,new TGLayoutHints(kLHintsExpandX | kLHintsExpandY | kLHintsLeft, 1, 1, 1, 1));
pBrowser->AddFSDirectory("/", “/”);

TRootBrowser *b = pBrowser->GetNewBrowser();
b->Connect(“DoubleClicked(TObject )",“MyClass”,this,"Checked(TObject,Bool_t)”);

But for a reason that I do not understand, I have the following message:

Error in TQObject::CheckConnectArgs: signal TGTransientFrame::DoubleClicked(TObject*) does not exist

How is it possible that for root, my TRootBrowser appears like a TGTransientFrame, and consequently is not able to make the signal connection ? an idea ?


This cannot work. And you’re over-complicating things… Why not trying with a simple TGFileContainer?
Anyway, I’ll check if I can do something with your example tomorrow, but I won’t advise to go to this direction anyway…

In the TGFileContainer, if you want to come back three folder back, you need to click three times on “…”, you never see the complete tree of your files and its very boring when I need to work on two different folders at the same time…

You could have two (or more) TGFileContainers… Anyway, I’ll check what can be done.

More details about why it doesn’t work. Looking at this line:

TRootBrowser *b = pBrowser->GetNewBrowser();

GetNewBrowser() returns fNewBrowser:

TRootBrowser *GetNewBrowser() const { return fNewBrowser; }

And fNewBrowser is defined as:

TGFileBrowser::TGFileBrowser(const TGWindow *p, TBrowser* b, UInt_t w, UInt_t h)
   : TGMainFrame(p, w, h), TBrowserImp(b), fNewBrowser(0)
   if (p && p != gClient->GetDefaultRoot())
      fNewBrowser = (TRootBrowser *)p->GetMainFrame();

So in your case, it returns the parent Windows (which usually is supposed to be a TRootBrowser…)

Then my next question is: why not doing the reverse and create a plugin (or several ones) to use your gui inside the ROOT browser (which is one of its major feature)?


Ok its clearer now why it was not working. Its a pity to have this TGFileBrowser without being able to get the signal on the selected files… anyway.

My gui is already existing actually, but it was at the beginning for reading specific root trees which were given in a conf file and used to plot histograms. I am just developing a new version which allows to open already existing histograms and that’s why I wanted to have this kind of browser. Including all my GUI in the right part of a TBrowser makes quite huge modifications only for this possibility to browse root files. in my idea, this option is only accessible by a menu which open a Browser to plot some histograms and then closing it. At the end I think I will only keep the solution with the TGFileContainers, but I’m a bit frustrated to not be able to have a real RootBrowser. That’s life. Thanks for your help


As I said, there are several solutions. It only depends on how much efforts you are ready to spend to embed a ROOT browser (which is much, much more than just a file browser) in your own GUI… And embedding your own GUI in the TBrowser is probably something like a few (less than ten) lines of code. As you prefer.

Yes sure but I have also a Menu file, different tabs… etc and it will not be very beautiful and user friendly to add it in a sub frame of the TBrowser. And I’m not sure it is easy to merge my tabs with the one of the TBrowser, and to merge my menu file with the one of the TBrowser… etc (see picture) Do you see what I mean ?