[ Pobierz całość w formacie PDF ]
.f.qc 11/13/00 14:11 Page 156Part I &' Getting Started156Grouping Check ButtonsA QCheckBoxis a button that can be toggled between off and on states.The state ismaintained inside the QCheckBoxitself.A check button is sometimes referred to as atoggle button.The following example creates the collection of check buttons shown inFigure 7-8.The checkmark only appears if a check button is in the on state.Figure 7-8: A group of QCheckBox buttons organized verticallyVertCheck Header1 /* vertcheck.h */2 #ifndef VERTCHECK_H3 #define VERTCHECK_H45 #include6 #include7 #include89 class VertCheck: public QWidget10 {11 Q_OBJECT12 public:13 VertCheck(QWidget *parent=0,const char *name=0);14 private:15 QVButtonGroup *group;16 QLabel *label;17 enum ButtonChoice { Total, Average,18 Maximum, Minimum, Exit };19 bool totalFlag;20 bool averageFlag;21 bool minimumFlag;22 bool maximumFlag;23 private slots:24 void slotButton(int ID);25 };2627 #endif 4682-1 ch07.f.qc 11/13/00 14:11 Page 157Chapter 7 &' Grouping Widgets157The QVButtonGroupis included as part of the class data, on line 15, because the slotthat receives button information only supplies the button ID number, making it nec-essary to query the groupfor the check button status.The current QCheckButtonstatus settings are stored in the Boolean variables defined on lines 19 through 22.VertCheck1 /* vertcheck.cpp */2 #include3 #include4 #include5 #include6 #include  vertcheck.h78 int main(int argc,char **argv)9 {10 KApplication app(argc,argv, vertcheck );11 VertCheck *vertcheck = new VertCheck();12 vertcheck->show();13 app.setMainWidget(vertcheck);14 return(app.exec());15 }1617 VertCheck::VertCheck(QWidget *parent,const char *name)18 : QWidget(parent,name)19 {20 QCheckBox *button;21 QVBoxLayout *layout = new QVBoxLayout(this,5);2223 group = new QVButtonGroup(this, vg1 );24 button = new QCheckBox( Total ,group);25 group->insert(button,Total);26 button = new QCheckBox( Average ,group);27 group->insert(button,Average);28 button = new QCheckBox( Maximum ,group);29 group->insert(button,Maximum);30 button = new QCheckBox( Minimum ,group);31 group->insert(button,Minimum);32 QPushButton *pButton = new QPushButton( Exit ,group);33 group->insert(pButton,Exit);34 connect(group,SIGNAL(clicked(int)),35 this,SLOT(slotButton(int)));36 layout->addWidget(group);3738 label = new QLabel(  ,this);39 layout->addWidget(label);4041 totalFlag = FALSE;42 averageFlag = FALSE;43 minimumFlag = FALSE;44 maximumFlag = FALSE; 4682-1 ch07.f.qc 11/13/00 14:11 Page 158Part I &' Getting Started1584546 resize(10,10);47 layout->activate();48 }49 void VertCheck::slotButton(int ID)50 {51 QButton *button = group->find(ID);52 switch(ID) {53 case Total:54 totalFlag = ((QCheckBox *)button)->isChecked();55 break;56 case Average:57 averageFlag = ((QCheckBox *)button)->isChecked();58 break;59 case Maximum:60 maximumFlag = ((QCheckBox *)button)->isChecked();61 break;62 case Minimum:63 minimumFlag = ((QCheckBox *)button)->isChecked();64 break;65 case Exit:66 kapp->exit(0);67 }68 QString string;69 if(totalFlag)70 string += QString( Tot  );71 if(averageFlag)72 string += QString( Avg  );73 if(maximumFlag)74 string += QString( Max  );75 if(minimumFlag)76 string += QString( Min  );77 label->setText(string);78 }The VertCheckconstructor, beginning on line 17, creates a vertical box container;and installs a QVButtonGroupfor its top widget and a QLabelfor its bottom widget.The address of the QVButtonGroupis stored as groupin the class.The call toaddWidget()makes the QVButtonGroupthe top widget of the QVBoxLayoutlayout manager.Lines 24 through 31 create the four QCheckBoxobjects and insert them into theQVButtonGroup.A fifth button  a standard pushbutton  is created and insertedinto groupon lines 32 through 35.On lines 38 and 39, a label used to display thetext at the bottom of the window is created and installed into the layout.The default condition of a QCheckBoxis off, which is represented as FALSE, so lines41 through 44 are used to set the four internal flags to the same values as the check 4682-1 ch07.f.qc 11/13/00 14:11 Page 159Chapter 7 &' Grouping Widgets159boxes.If you wish to preset one or more of the check boxes to being initially on, itcould be done like this:button->setChecked(TRUE);You would then set its corresponding Boolean value to TRUE [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • elanor-witch.opx.pl
  •