[Qt-interest] QRadioButton setDown(true) initialise problem.

Mike Davis mike at miketdavis.blogsite.org
Thu Jan 7 20:27:35 CET 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try setChecked(true) instead. Since both radio buttons are children of
the top widget, they are exclusive to each other and you shouldn't
observe any odd behavior. If you need multiple sets of exclusive radio
button groups on the same widget, you can use a QButtonGroup like this.

#include <QApplication>
#include <QtGui>

int main(int argc, char *argv[])
{
  QApplication app(argc,argv);
  QWidget *main = new QWidget;
  QVBoxLayout *lyt = new QVBoxLayout;
  QButtonGroup *grp = new QButtonGroup;
  QButtonGroup *grp2 = new QButtonGroup;
  QRadioButton *q1 = new QRadioButton("Grp 1 Btn 1");
  QRadioButton *q2 = new QRadioButton("Grp 1 Btn 2");
  QRadioButton *q3 = new QRadioButton("Grp 2 Btn 1");
  QRadioButton *q4 = new QRadioButton("Grp 2 Btn 2");

  //Add buttons to group 1
  grp->addButton(q1);
  grp->addButton(q2);

  //Add buttons to group 2
  grp2->addButton(q3);
  grp2->addButton(q4);

  //Add buttons to layout
  lyt->addWidget(q1);
  lyt->addWidget(q2);
  lyt->addWidget(q3);
  lyt->addWidget(q4);

  //Set layout, set state
  main->setLayout(lyt);
  q1->setChecked(true);
  main->show();
  return app.exec();
}


Mike Davis

On 1/7/2010 9:04 AM, phil prentice wrote:
> Hi
>   I have an application where I need to initialise a radiobuttons state at 
> startup.  If I create the button and then set down to true, when I go and 
> select other radiobuttons using the mouse (buttons have same parent)the radio 
> button that I originally pressed down via setDown() will remain down until I 
> use the mouse to select it then all the buttons work fine(exclusively) as 
> expected.
> 
> Heres a bit of code to demonstrate (although to be honest this seems to have 
> even more bizaire consequences, but thats another problem).
> 
> #include <QApplication>
> #include <QtGui>
> int main(int argc, char *argv[])
> {
>   QApplication app(argc,argv);
>   QWidget *top = new QWidget();
>   QRadioButton *q1 = new QRadioButton("button1",top);
>   QRadioButton *q2 = new QRadioButton("button2",top);
>   q1->setDown(true);
>   q2->setDown(true);
>   q1->move(5,10);
>   q2->move(5,40);
>   top->show();
>   return app.exec();
> }
> 
> I get some strange results when I run the above which I dont understand.
> When I first see the displayed widget I see both radio buttons down..which is 
> wrong. However if I simply move my mouse the first button is changed to being 
> up (which is what I would have expected to see when it was first 
> displayed???).
> 
> Anyway I now move on to my main problem which is that when I select button1 
> with the mouse button 2 remains down.  Its not until I select button2 with 
> the mouse that both buttins start behaving as they should do.
> 
> I know the above code is a bit basic, but I just wanted to demonstarte my 
> problem(s).
> I'm using QT 4.2.1 which I appreciate is a bit out of date now, but I'm sure 
> its my understanding that is at fault here.
> What am I doing wrong?
> Thanks for your help.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (MingW32)

iEYEARECAAYFAktGNacACgkQyv8e+QfNYNuehwCeJmotG0fT2vcHzlCfplSj2T48
6vQAoJRFG+rua8j+J7c0ROEi1QMOTpXm
=dkny
-----END PGP SIGNATURE-----



More information about the Qt-interest-old mailing list