Loop over TLegend entries

Hello,

I have a constructed a TLegend object with sereral entries.

It is possible to loop over the entries, get a pointer to TLegendEntry
and then modify it ?

Does somebody has an example ?

Looking at the code, I see method that should in principle allow to do that,
but it looks rather design towards interactive use.

Regards,

      Tancredi

Hi Tancredi,

what you would need to do is using TLegend::GetListOfPrimitives. This gives you a TList with all the entries.

Benedikt

This is a duplicate of:

Example:

// TLegend* l was already defined and drawn
if(l==NULL) return;
TIter next(l->GetListOfPrimitives());
TLegendEntry* le;
UInt_t n = 1;
while((le=(TLegendEntry*)next()))
{
	le->SetOption("l");
	n++;
}

Alternatively:

TLegendEntry *tle = (TLegendEntry *)listA->At(2);

Just to add something to the absolutely correct answer of ferhue and Benedikt, if you are using a version of ROOT6 you can do


auto primitives = l->GetListOfPrimitives();
for (auto primitiveObj :  *primitives){
    auto primitive = (TLegendEntry*)primitiveObj;
    primitive->SetOption("l");
}

D