All code is run under qt3.3.8 + fedora8. Qbuttongroup is used to manage a set of qbutton buttons in a unified manner. Theoretically, all button classes inherited from qbutton can be used. Qpushbutton and qradiobutton are used as examples. The function of button group is mainly used in project selection. You can select one or multiple buttons Based on the attribute settings. Header file: <qbuttongroup. h> Main attributes and functions: Int ID: Button label. You can use this label to obtain or operate the buttons in the group. Bool exclusive: This property holds whether the button group is exclusive. If this property is true, then the buttons in the group are toggled, And to untoggle a button you must click on another button in the group. The default value is false. Set this property's value with setexclusive () and get this property's value with isexclusive (). Exclusive attribute It indicates whether only one button in the button group is activated. If this attribute is true, all buttons in the group are toggled (this word is not translated). to cancel a button, you must click another button. The default value is false. Set this property using the setexclusive () function; obtain isexclusive (). Insert (qbutton * button, int id =-1) Add a button to the button group. Setbutton (ID) Make the button represented by ID selected Complete Test code: # Ifndef groupbuttonpage_h # Define groupbuttonpage_h # Include <qpushbutton. h> # Include <qradiobutton. h> # Include <qlabel. h> # Include <qbuttongroup. h> # Include <qwidget. h> # Include <qlayout. h> Class groupbuttonpage: Public qwidget { Q_object Public: Groupbuttonpage (qwidget * parent = 0, const char * name = 0, wflags f = 0): qwidget (parent, name, F) { Qvboxlayout * vbox = new qvboxlayout (this ); Vbox-> setspacing (10 ); Setradiobuttongroup (vbox ); Setpushbuttongroup (vbox ); };
Private slots: Void resetradiobuttongroup (int id ); Void resetpushbuttongroup (int id ); PRIVATE: Qbuttongroup * m_radiobuttongroup; Qbuttongroup * m_pushbuttongroup;
Void setradiobuttongroup (qlayout * l ); Void setpushbuttongroup (qlayout * l ); }; Inline void groupbuttonpage: setradiobuttongroup (qlayout * l ){ M_radiobuttongroup = new qbuttongroup (this ); Connect (m_radiobuttongroup, signal (clicked (INT), this, slot (resetradiobuttongroup (INT ))); // M_radiobuttongroup-> setexclusive (true); // The buttons are mutually exclusive, that is, only one selected status can be selected at the same time. M_radiobuttongroup-> hide (); // hide the buttongroup (only show the button) // Qbuttongroup does not have layout management. It must be implemented using the layout class. Qhboxlayout * hbox = new qhboxlayout (L ); Hbox-> setspacing (5 ); // Hbox-> addwidget (New qlabel ("radiobutton button group:", this )); Hbox-> addwidget (m_radiobuttongroup ); // Put three radiobutton pairs in the buttongroup Qradiobutton * btn1 = new qradiobutton ("button 1", this, "radiobutton1 "); Hbox-> addwidget (btn1 ); M_radiobuttongroup-> insert (btn1 ); Qradiobutton * btn2 = new qradiobutton ("button 2", this, "radiobutton2 "); Hbox-> addwidget (btn2 ); M_radiobuttongroup-> insert (btn2 ); Qradiobutton * btn3 = new qradiobutton ("button 3", this, "radiobutton3 "); // Btn3-> settogglebutton (true); // qradiobutton: settogglebutton () is protected, because the default value is true. Hbox-> addwidget (btn3 ); M_radiobuttongroup-> insert (btn3 ); // Set the first button to be selected M_radiobuttongroup-> setbutton (0 ); } Inline void groupbuttonpage: setpushbuttongroup (qlayout * l ){ M_pushbuttongroup = new qbuttongroup (this ); Connect (m_pushbuttongroup, signal (clicked (INT), this, slot (resetpushbuttongroup (INT ))); M_pushbuttongroup-> setexclusive (true); // The buttons are mutually exclusive, that is, only one of them can be selected. M_pushbuttongroup-> hide (); // hide the buttongroup (only show the button) // Qbuttongroup does not have layout management. It must be implemented using the layout class. Qhboxlayout * hbox = new qhboxlayout (L ); Hbox-> setspacing (5 ); // Hbox-> addwidget (New qlabel ("Pushbutton button group:", this )); Hbox-> addwidget (m_pushbuttongroup ); // Put three Pushbutton instances in the buttongroup Qpushbutton * btn1 = new qpushbutton ("button 1", this, "pushbutton1 "); Btn1-> settogglebutton (true ); Hbox-> addwidget (btn1 ); M_pushbuttongroup-> insert (btn1 ); Qpushbutton * btn2 = new qpushbutton ("button 2", this, "pushbutton2 "); Btn2-> settogglebutton (true ); Hbox-> addwidget (btn2 ); M_pushbuttongroup-> insert (btn2 ); Qpushbutton * btn3 = new qpushbutton ("button 3", this, "pushbutton3 "); Btn3-> settogglebutton (true ); Hbox-> addwidget (btn3 ); M_pushbuttongroup-> insert (btn3 ); // Set the first button to be selected M_pushbuttongroup-> setbutton (0 ); } Inline void groupbuttonpage: resetradiobuttongroup (int id ){ M_radiobuttongroup-> setbutton (ID ); Qdebug ("groupbuttonpage: resetradiobuttongroup: Id = % d", ID ); Qdebug ("groupbuttonpage: resetradiobuttongroup: Name = % s", m_radiobuttongroup-> selected ()-> name ()); } Inline void groupbuttonpage: resetpushbuttongroup (int id ){ M_pushbuttongroup-> setbutton (ID ); Qdebug ("groupbuttonpage: resetpushbuttongroup: Id = % d", ID ); Qdebug ("groupbuttonpage: resetpushbuttongroup: Name = % s", m_pushbuttongroup-> selected ()-> name ()); } # Endif // groupbuttonpage_h |