TGRadioButtons in 2x2 grid?

How can I create a button group that is a 2x2 grid, instead of vertical or horizontal?

I have 4 buttons, and I can organize them in a single row, or a single column, but what I want is 2 rows of 2 buttons each:

Here’s a simple jpg of what I have and what I want.

Thanks for you help,

Hi buddy,

Maybe something like this:void test_matrix() { TGRadioButton *fR[4]; TGMainFrame *mf = new TGMainFrame(gClient->GetRoot(), 300, 170); TGGroupFrame *fG1 = new TGGroupFrame(mf, new TGString("Valves modes")); fG1->SetLayoutManager(new TGMatrixLayout(fG1, 2, 2, 10, 10)); fR[0] = new TGRadioButton(fG1, new TGHotString("Normal")); fR[1] = new TGRadioButton(fG1, new TGHotString("Hold")); fR[2] = new TGRadioButton(fG1, new TGHotString("Vent")); fR[3] = new TGRadioButton(fG1, new TGHotString("Tare")); for (int i = 0; i < 4; ++i) fG1->AddFrame(fR[i], new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)); mf->AddFrame(fG1, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)); mf->MapSubwindows(); mf->Layout(); mf->MapWindow(); }
Cheers, Bertrand.


Thanks for that snippet of code.

I do have a problem when I try to change the font (to match the rest of the GUI); the little circle indicating a radio button disappears from the screen. Perhaps a bug in calculating the text size due to the font change?

To select the font, I use:
//make a bigger font for text buttons
TGFontPool *pool = gClient->GetFontPool();
TGFont *font = pool->GetFont(“fixed”, -16, kFontWeightNormal, kFontSlantRoman);
FontStruct_t ft = font->GetFontStruct();

and after creating the new radio button, using pointer nTB, I have:


Hi buddy,

Try to add a call to TGRadioButton::Resize(TGRadioButton::GetDefaultSize()) before adding it to the group frame, as shown below: nTB->SetFont(ft); nTB->Resize(nTB->GetDefaultSize()); nGroupFrame->AddFrame(nTB); // where you add the button to the group frame...
And BTW, you can also use TGTableLayout instead of TGMatrixLayout…

Cheers, Bertrand.

Thanks again for your help.
Adding the Resize() call made everything all better.


Well, now that I’m actually running this new code, I had a problem, in that the buttons have lost their ‘radio-button’ functionality.

When one radio button was clicked, it’s little circle became filled in, but it did not clear the little circle for the other radio buttons in this group. So you end up with more than 1 radio button appearing to be currently active.

I fixed this by using a TGButtonGroup instead of a TGGroupFrame, and no need to explicitly ->AddFrame(), as evidently that is handled automatically for ButtonGroups.