在Qt GUI中,单选框类是QRadioButton,复选框类是QCheckBox。它们都是QAbstractButton的派生类。
(1)单选框
//设置为选择状态
radioButton->setChecked(true);
//返回选择状态
bool radio_sel = radioButton->isChecked();
(2)复选框
复选框不同于单选框,它有三种状态:checked、unchecked和PartiallyChecked。
Qt::Unchecked 0 The item is unchecked.
Qt::PartiallyChecked 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
Qt::Checked 2 The item is checked.
常用方法和属性如下:
[cpp] view plaincopy
//返回选择的状态
Qt::CheckState checkState () const
//是否处于PartiallyChecked状态
bool isTristate () const
//设置选择的状态,checked或unchecked
void setCheckState ( Qt::CheckState state )
//设置为PartiallyChecked状态
void setTristate ( bool y = true )