group_one = new QButtonGroup (this, "group_one");
group_one->setGeometry (QRect (0, 0, 110, 121));
group_one->setTitle (tr ("Group"));
radio_one = new QRadioButton (group_one, "radio_one");
radio_one->setGeometry (QRect (10, 20, 90, 40));
radio_one->setText (tr ("One"));
radio_one->setChecked (TRUE);
radio_two = new QRadioButton (group_one, "radio_two");
radio_two->setGeometry (QRect (10, 70, 90, 40));
radio_two->setText (tr ("Two"));
// 设定第一个 buttongroup, 并且在 buttongroup 上面设定 radio button 1 和 radio button 2
// radio_one 的 default 是 checked (已经被选择的)
group_two = new QGroupBox (this, "group_two");
group_two->setGeometry (QRect (0, 120, 111, 201));
group_two->setTitle (tr ("Group 2"));
check_one = new QCheckBox (group_two, "check_one");
check_one->setGeometry (QRect (10, 20, 90, 40));
check_one->setText (tr ("One"));
check_one->setChecked (TRUE);
check_two = new QCheckBox (group_two, "check_two");
check_two->setGeometry (QRect (10, 80, 90, 40));
check_two->setText (tr ("Two"));
check_three = new QCheckBox (group_two, "check_three");
check_three->setGeometry (QRect (10, 140, 90, 40));
check_three->setText (tr ("Three"));
// 第二个 group 我们改用 groupbox 来作范例. 并且在 group box 上面
// 生成三个 check box , check box 1, check box 2 和 check box 3
// check_one 的 default 是 checked (已经被选择的)
ok_one = new QPushButton (this, "ok_one");
ok_one->setGeometry (QRect (120, 30, 71, 61));
ok_one->setText (tr ("OK"));
// 第一个 ok button, 我们用来检查 radio button 的状态
ok_two = new QPushButton (this, "ok_two");
ok_two->setGeometry (QRect (120, 190, 71, 61));
ok_two->setText (tr ("OK"));
// 第二个 ok button, 用来检查 check box 的状态
click_label = new QLabel (this, "click_label");
click_label->setGeometry (QRect (250, 270, 190, 41));
QFont click_label_font (click_label->font ());
click_label_font.setPointSize (1
; click_label->setFont (click_label_font);
click_label->setText (tr ("Click On OK"));
click_label->setAlignment (int (QLabel::AlignCenter));
// 在 LCD 的数字上方显示一些字. 这里我们显示的是 Click On Ok
// 也就是说, LCD 是显示的 ok button 被 mouse clicked 过的次数.
// 这里我们只显示 ok_one 和 ok_two, 简单的来说,就是显示 radio button
// 和 check box 的状态一共被检查过几次
picture = new QLabel (this, "picture");
picture->setGeometry (QRect (480, 10, 110, 140));
picture->setText (tr ("Picture"));
QPixmap pixmap ("logo.xpm");
picture->setPixmap (pixmap);
picture->setScaledContents (TRUE);
// 我用 convert 这苹程式,把那个企鹅的 logo (logo.gif) 转换成了 logo.xpm
// 这里我们把这个 loggo 读进来. 并且让 picture 这个 QLabel 作为我们的显示
// 对象. 这样我们的程式里面就有了一苹象徵 Linix 的小企鹅了.
LineEdit = new QLineEdit (this, "LineEdit");
LineEdit->setGeometry (QRect (210, 30, 251, 51));
LineEdit->setReadOnly (TRUE);
// 我们这里用 lineedit 用来显示 radio button
// 所以我们把 LineEdit 设定为 setReadOnly (TRUE) 只读
MultiLineEdit = new QMultiLineEdit (this, "MultiLineEdit");
MultiLineEdit->setGeometry (QRect (210, 150, 250, 90));
MultiLineEdit->setReadOnly (TRUE);
// 我们用 MultiLineEdit 来显示 check box 的状态
// 同样的道理,这里我们也不需要输入,所以设定 MultiLineEdit 为只读
LCD = new QLCDNumber (this, "LCD");
LCD->setGeometry (QRect (220, 320, 231, 91));
LCD->setNumDigits (10);
LCD->setProperty ("intValue", 0);
// 因为考虑到我们会使用 bin (二进位)的方法来显示
// 所以把显示的最大位设置为 10 位 LCD->setNumDigits (10)
// 并且设定初始值为 0
// LCDNumber 的内定显示是 DEC 的. (十进位)
group_three = new QButtonGroup (this, "group_three");
group_three->setGeometry (QRect (470, 170, 111, 221));
group_three->setTitle (tr ("LCD"));
dec = new QRadioButton (group_three, "dec");
dec->setGeometry (QRect (10, 60, 81, 21));
dec->setText (tr ("Dec"));
dec->setChecked (TRUE);
oct = new QRadioButton (group_three, "oct");
oct->setGeometry (QRect (10, 90, 81, 31));
oct->setText (tr ("OTC"));
bin = new QRadioButton (group_three, "bin");
bin->setGeometry (QRect (10, 120, 91, 31));
bin->setText (tr ("Bin"));
hex = new QRadioButton (group_three, "hex");
hex->setGeometry (QRect (10, 30, 81, 21));
hex->setText (tr ("Hex"));
lcd_ok_button = new QPushButton (group_three, "lcd_ok_button");
lcd_ok_button->setGeometry (QRect (10, 160, 91, 51));
lcd_ok_button->setText (tr ("OK"));
// 在 LCD 旁边再建立一个 group ( Button Group ) 里面包含了 radio button 和 bush button
// radio button 有 dec, oct, bin, hex 这四项. 用来选择 LCD 的显示模式 (8进位,10进位,16进位,2进位)
// lcd_ok_button 是一个 push button. 最为选择 LCD 显示模式的 『确定』键
clear_button = new QPushButton (this, "cler_button");
clear_button->setGeometry (QRect (10, 420, 131, 41));
clear_button->setText (tr ("Clear All"));
// 这个显示为 clear all 的 push button, 用来把
// 所有的值都设定到原始状态
exit_button = new QPushButton (this, "exit_button");
exit_button->setGeometry (QRect (430, 420, 131, 41));
exit_button->setText (tr ("Exit"));
// 退出程式用的 push button
connect (ok_one, SIGNAL (clicked ()), this, SLOT (check_radio ()));
// 连接一个信号,如果 ok_one 被 click 了,就去执行 check_radio()
connect (ok_two, SIGNAL (clicked ()), this, SLOT (check_box ()));
// 连接一个信号,如果 ok_two 被 click 了, 就去执行 check_box()
connect (lcd_ok_button, SIGNAL (clicked ()), this, SLOT (check_lcd ()));
// 连接一个信号,如果 lcd_ok_button 被 click 了,就去执行 check_lcd()
connect (clear_button, SIGNAL (clicked ()), this, SLOT (CLEAR ()));
// 连接一个信号,如果 clear_button 被 click 了,就去执行 CLEAR ()
connect (exit_button, SIGNAL (clicked ()), kapp, SLOT (quit ()));
// 连接一个信号,如果 exit_button 被 click 了, 就把我们的整个程式关闭掉
}
Final::~Final () //
{
}
void Final::check_radio ()
{
i++;
if (radio_one->isChecked ())
LineEdit->setText (tr ("Radio Button 1 is Checked"));
if (radio_two->isChecked ())
LineEdit->setText (tr ("Radio Button 2 is Checked"));
LCD->display (i);
}
// 在 check_radio () 中.我们用 isCheck() 来检查 radio button 的状态
// 如果 radio button 是被选择的,那麽 isCheck() 将会返回 TRUE
// 如果 radio button 没有被选择,则返回 FALSE 的值
// 如果说 radio_one 有被选择,我们就用 setText() 在 LineEdit 中显示 Radio Button 1 is Clicked
// 这几个字
// 如果 radio_two 有被选择,我们就用 setText() 在 LineEdit 中显示 Radio Button 2 is Clicked
//
// 在一开始,有个 i++, 这是我们用来统计 ok_one 和 ok_two 被 clicked 的次数用的
// 如果进入 check_radio() 就证明, ok_one 已经被 click 过一次.所以我们把 i+1,
// 然後用 display() 在 LCD 上面显示 i 的值
void Final::check_box ()
{
i++;
if (check_one->isChecked ())
CHECK = CHECK + "Check Box 1 is Checked ";
if (check_two->isChecked ())
CHECK = CHECK + "Check Box 2 is Checked ";
if (check_three->isChecked ())
CHECK = CHECK + "Check Box 3 is Checked ";
MultiLineEdit->setText (CHECK);
CHECK = "";
LCD->display (i);
}
// check box 这里看上去稍稍比 radio button 复杂一些. 因为 radio button 我们只能选择
// 一个. 要麽就是 radio_one, 要麽就是 radio_two. 所以用个 LineEdit 就可以搞定.
// 但是 check box 是可以多项选择的.也就是说既可以是 check box1 单一的被选择. 也可以
// 是 check box 1 + check box 2, 还可以 check box 1 + check box 2 + check box 3.又或者
// check box 2 + check box 3 等等.所以这里我们需要用到 MultiLineEdit 来显示多行的信息.
// 而信息就放在我们的 QString 中 (CHECK)
// 我们用 isChecked() 来检查 check_one, 如果 check_one 被选择
// CHECK (我们的QString)就等於 CHECK+"Check Box 1 is Checked ", 那麽 CHECK (QString)
// 本身是空的.所以这里 CHECK 就等於 Check Box 1 is Checked 这几个字.後面的 是 10, 也
// 就是 换行符号的意思
// 同样的,如果 check_two 有被选择,那麽 CHECK 就等於 CHECK+"Check Box 2 is Checked ",
// 这时侯就是两种情况. 1. check_one 没有被选择,所以这时侯,我们的 String Check 就是空
// 的+"Check Box 2 is Checked " 这几个字. 而如果 check_one 是有被选窄的.那麽 String
// CHECK 本身已经包含了 "Check Box 1 is Checked "的字.这时侯在加上 "Check Box 2 is
// Checked " 这几个字. 那麽 String CHECK 就有两行得内容了.
// check_three 的道理跟 check_two 是一样的. 只不过这时侯由两种可能变成了四种而已
// 1. check_one clicked + check_two clicked
// 2. check_one clicked
// 3. check_two clicked
// 4. check_one 和 check_two 都没有被 click
// 然後我们用 setText 把 String CHECK 显示到我们的 MultiLineEdit 上面去.
// 并且把 CHECK 中的 String 清除. (下次检查状态的时侯还要用到)
// 因为 ok_two 被 click 过了,所以我们的 i 又加了 1, 并且在 LCD 中显示出来
void Final::check_lcd ()
{
if (dec->isChecked ())
{
LCD->setMode (QLCDNumber::DEC);
}
if (hex->isChecked ())
{
LCD->setMode (QLCDNumber::HEX);
}
if (oct->isChecked ())
{
LCD->setMode (QLCDNumber::OCT);
}
if (bin->isChecked ())
{
LCD->setMode (QLCDNumber::BIN);
}
}
// 这是最间单的一个了
// 检查∶
// dec 有被选择, 将 LCD 显示模式改变为 DEC
// hex 有被选择, 将 LCD 显示模式改变为 HEX
// oct 有被选择, 将 LCD 显示模式改变为 OCT
// bin 有被选择, 将 LCD 显示模式改变为 BIN
void Final::CLEAR ()
{
LineEdit->clear ();
MultiLineEdit->clear ();
radio_one->setChecked (TRUE);
dec->setChecked (TRUE);
check_one->setChecked (TRUE);
check_two->setChecked (FALSE);
check_three->setChecked (FALSE);
LCD->setMode (QLCDNumber::DEC);
i = 0;
LCD->setProperty ("intValue", 0);
}
// 这里我们把所有选项变回程式开始的原始状态.
// 并且把显示的状态情况清空, LCD 设定回 0
// LineEdit->clear () 通过呼叫 clear() 把 LineEdit 清空
// MultiLineEdit->clear () 一样的道理. 用 clear() 清空
// 用 setChecked() 来把 radio_one 设定为『已选择』
// 用 setChecked() 把 dec 设定为 『已选择』
// 对於 radio button 来说.比较方便.因为我们从众多选择中
// 只能选择一个. 但是对於 check box 就不一样了. check box
// 可以被任意的多项选择.所以我们只好把每一个 check box 的
// 状态都设定一遍
// check_one->setChecked ( TRUE ) check_one 设定为『已选择』
// check_two->setChecked ( FALSE ) check_two 设定为没有被选择.
// check_three->setChecked ( FALSE ) check_three 设定为没有被选择
// LCD->setMode (QLCDNumber::DEC) 把 LCD 的显示模式设定为 10 进位
// i=0; 把 LCD 的计数器设定为 0
// LCD->setProperty ("intVale", 0); 将 LCD 初始化,初始值为 0
