[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: how do I implement dynamic radio buttons in a dialog?
From:       "Jaldhar H. Vyas" <jaldhar () debian ! org>
Date:       2006-06-11 18:24:08
Message-ID: Pine.LNX.4.58.0606111314510.4229 () jaldhar ! brainfood ! com
[Download RAW message or body]

On Wed, 7 Jun 2006, Jaldhar H. Vyas wrote:

> Any help with this problem would be gratefully received.
>
> In a dialog box I need to dynamically create radio buttons at runtime
> based on a variable number of items.
>
> In the following code, ui is the class derived from my .ui file. items as
> you can see is a QMap<QString, int>.  The keys of this QMap should
> be the labels of my radio buttons.  btngrp is is a QButtonGroup which
> should contain my radio buttons vertically.  (Related question: why won't
> Qt Designer let me create a QVButtonGroup?)
>
>
> for (QMap< QString, int >::const_iterator it = _items.begin();
>     it != _items.end(); it++)
> {
>    int id = ui->btngrp->insert(new QRadioButton(it.key(),
>      reinterpret_cast<QWidget *>(ui->btngrp->parent())));
>    ui->btngrp->find(id)->show();
> }
>
> This works but the newly created radio buttons are not arranged neatly but
> stacked on top of each other in the top left of the button group.  What to
> do I need to do to make them appear in a single column?  Is it because I
> didn't use a QVButtonGroup?  Do my radio buttons have the wrong parent?
> Or is it something else altogether?
>

Never mind, thanks to the good people of #kde-devel on IRC, I got this
problem resolved.

 // The following three lines were the key to proper button arrangement.
 ui->btngrp->setColumnLayout(0, Qt::Vertical);
 QVBoxLayout *btngrplayout = new QVBoxLayout(ui->btngrp->layout());
 btngrplayout->setAlignment( Qt::AlignTop );

 for (QMap< QString, int >::const_iterator it = _items.begin();
     it != _items.end(); it++)
 {
   // None of that nasty reinterpret cast business nexessary.
   QRadioButton* b = new QRadioButton(it.key(), ui->btngrp);
   int id = ui->btngrp->insert(b);
   btngrplayout->addWidget(b);
 }

-- 
Jaldhar H. Vyas <jaldhar@debian.org>
La Salle Debain - http://www.braincells.com/debian/
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic