打开QT creator:在右方控件栏中拖拽构建界面。
构建界面如下:
然后右键按钮,选择编辑槽,转到按钮点击事件的编辑。
选择信号:
转到代码编辑:
点击事件函数:
void Dialog::on_caculateButton_clicked()
{
QString estr1 = "can not devide 0!";
QString estr2 = "please choose caculate option!";
double n1 = ui->num1lineEdit->text().toDouble();
double n2 = ui->num2lineEdit->text().toDouble();
if( (abs(n1-0) < 1E-10 && ui->num1lineEdit->text().size()!=1)
|| (abs(n2-0) < 1E-10 && ui->num2lineEdit->text().size()!=1) )
{
ui->reslineEdit->setText("Please Input numbers!");
return ;
}
double res = 0.;
if ( ui->mutRadioButton->isChecked() )
{
res = n1 * n2;
}
else if( ui->pluRadioButton->isChecked() )
{
res = n1 + n2;
}
else if( ui->subRadioButton->isChecked() )
{
res = n1 - n2;
}
else if( ui->devRadioButton->isChecked() )
{
if( abs(n2 - 0) < 1E-10 )
{
ui->reslineEdit->setText( estr1 );
return ;
}
else
{
res = n1 / n2;
}
}
else
{
ui->reslineEdit->setText(estr2 );
return;
}
ui->reslineEdit->setText( QString::number( res, 'f', 10));
//ui->num1lineEdit->setText("");
//ui->num2lineEdit->setText("");
}
除此之外,还可以添加其他控件。运行效果。