PDA

View Full Version : QGroupBox formatting



manaila
16th February 2012, 07:49
I have a QGroupBox of radio buttons. I have added the buttons to the horizontal layout in the group box using:
layout->addWidget(RadioButton1, 0, Qt::AlignHCenter); so they are centered in the group box. I have also made the groupbox title to be centered.
Unfortunately, it appears that the radio button AND its corresponding text are centered and this makes them to be not properly formated vertically when the button texts are of different string lengths. I had to add some empty spaces after the text in order to adjust its size so it is aligned vertically with other buttons, but this seems to be very inconvenient.
Also the the group box does not get centered and the borders not shown.
I'm using Ubuntu 11.04; on Windows the borders are shown with the title centered.

Can anyone help me with the problems I am having.

Gokulnathvc
16th February 2012, 07:55
kindly refer
http://www.qtcentre.org/threads/40226-user-resize-QGroupBoxes-size-dynamically-with-the-mouse

http://stackoverflow.com/questions/4306019/qgroupbox-border

ChrisW67
16th February 2012, 22:28
It sounds like what you want is the left-aligned radio buttons to be centred inside the group box. You can do that using a horizontal layout containing three things:

An expanding horizontal spacer
A vertical layout containing the radio buttons (left aligned)
An expanding horizontal spacer

Like this:


QGroupBox *groupBox = new QGroupBox("Group", this);
groupBox->setAlignment(Qt::AlignHCenter); // title alignment only

QRadioButton *rb1 = new QRadioButton("Radio", this);
QRadioButton *rb2 = new QRadioButton("Another radio", this);

QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(rb1);
vLayout->addWidget(rb2);

QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding) );
hLayout->addLayout(vLayout);
hLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding) );

groupBox->setLayout(hLayout);



Also the the group box does not get centred and the borders not shown.
Centred in what? Setting the title alignment does not affect the placement of the group box itself. You can use the same technique as above to centre the group box in a containing layout if that is what you mean.

The absence or otherwise of the group box border is driven by the style in use, which varies from platform to platform by default. You can use style sheets to adjust the appearance of different widgets.