[Interest] How to produce a cluster of radio buttons?

David Boosalis david.boosalis at gmail.com
Fri Aug 31 06:36:30 CEST 2012


Another way is to use QToolButton instead of QCheckBox.

For each button do something like this:

 QIcon Icon;
  QPixmap pm(":/images/off.png");
  QPixmap pmSelected(":/images/on.png");
  Icon.addPixmap(pm,QIcon::Normal,QIcon::Off);
  Icon.addPixmap(pmSelected,QIcon::Normal,QIcon::On);

QToolButton *currentButton = 0;
for (i = 0; i< numOfButtons;i++)
{
    button[i]    = new QToolButton();
    button[i]->setCheckable(true);
    button->setIcon(Icon);
    button->setFixedSize(pm.size());
    connect(button[i],SIGNAL(clicked()), this,SLOT(buttonSelected()));

}


 Don't think you can use QButtonGroup with QToolButton and rely on its
exclusivity property, but you can keep a pointer to what was last used like
this :

MyClass::buttonSelected()
{
 QToolButton *thisButton = sender();
 if (currentButton) {
   disconnect(currentButton,SIGNAL(clicked()),
this,SLOT(buttonSelected())); // may have to do this so as not to emit
signal
   currentButton->setChecked(false);
   connect(currentButton,SIGNAL(clicked()), this,SLOT(buttonSelected()));
}
currentButton = thisButton;
...
}

Hope this is of some value
David

On Thu, Aug 30, 2012 at 4:37 PM, Shriramana Sharma <samjnaa at gmail.com>wrote:

> Hello. I'm trying to produce a layout similar to the attached, where
> nine radio buttons are set out in a 3x3 cluster. I am producing the
> QRadioButton-s and trying stuff like setSizePolicy ( QSizePolicy ::
> Fixed, QSizePolicy :: Fixed ), resize ( height (), height () ) etc but
> all I get is radio buttons which are horizontally very much spaced
> away from each other (the vertical spacing is OK).
>
> Can anyone please tell me how to set the width of the radio button
> same as the height (since only the button part and no label is being
> displayed) and hwo to ensure that the components of this cluster are
> fixed w.r.t one another when the window is resized?
>
> Thanks.
>
> --
> Shriramana Sharma
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120830/b9ff348a/attachment.html>


More information about the Interest mailing list