PDA

View Full Version : array of radio buttons



amulya
5th October 2006, 11:53
hi there,

i m trying to add an array of radio buttons in a grid layout and then further into a widget. but these radio buttons seem to overlap over each other. Thus in the output, there is only one radio button. Here is the code


KPH_WIDGET::KPH_WIDGET(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
QPalette objPalette1;
objPalette1.setColor(QPalette::Window,Qt::white);
QPalette objPalette2;
objPalette2.setColor(QPalette::Window,Qt::gray);

//setFixedSize(300,300);
QGridLayout* pGridLayout = new QGridLayout(this);

CRadioButtonEx* arr[10] ;
for(int i = 0; i < 10; i++)
{
pGridLayout->setRowMinimumHeight(i,50);
arr[i] = new CRadioButtonEx(tr("check"),this);

if(arr[i])
{
if(i%2 == 0)
arr[i]->setPalette(objPalette1);
else
arr[i]->setPalette(objPalette2);

arr[i]->setAutoFillBackground(true);


}

pGridLayout->addWidget(arr[i],i,0,Qt::AlignLeft);
}

Here, CRadioButtonEx is a customized radio button which works correctly. On debugging, it shows that all the 10 of the radio buttons are created. But only one is shown.

Looking forward to a reply.

Cheers,
Amulya

high_flyer
5th October 2006, 11:59
try arr[i]->show(); after/before adding to layout.

amulya
5th October 2006, 12:11
hi there,

thanx for ur reply.
it shows the same output i.e. only one radio button.
i also tried to do it with normal QRadioButton..that also doesnt work

any other suggestions??

Amulya

Gopala Krishna
5th October 2006, 12:16
Hello ,
You are trying to set your own layout in QMainWidget. Your are not supposed to do that.
Instead you should create a widget and set it as central widget of QMainWindow. You can use your own layout in centralWidget.
So try this code

QPalette objPalette1;
objPalette1.setColor(QPalette::Window,Qt::white);
QPalette objPalette2;
objPalette2.setColor(QPalette::Window,Qt::gray);
//added these two lines and modified one line
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QGridLayout* pGridLayout = new QGridLayout(centralWidget);

QRadioButton* arr[10] ;
for(int i = 0; i < 10; i++)
{
pGridLayout->setRowMinimumHeight(i,50);
arr[i] = new QRadioButton(tr("check"),this);
if(arr[i])
{
if(i%2 == 0)
arr[i]->setPalette(objPalette1);
else
arr[i]->setPalette(objPalette2);
arr[i]->setAutoFillBackground(true);
}
pGridLayout->addWidget(arr[i],i,0,Qt::AlignLeft);
}
Hope this helps !

amulya
5th October 2006, 12:59
hi gopala,

i knew i was missing smth..anyways..u rock..problem solved

thanx again

Cheers,
Amulya