qt程序详解

Calculator::Calculator(QWidget*parent):QDialog(parent){first=0;second=0;operat=-1;edi... Calculator::Calculator(QWidget *parent) : QDialog(parent)
{
first = 0;
second = 0;
operat = -1;
edit = new QLineEdit("0");
edit->setReadOnly(true);
button[0] = new QPushButton("0");
button[1] = new QPushButton("1");
button[2] = new QPushButton("2");
button[3] = new QPushButton("3");
button[4] = new QPushButton("4");
button[5] = new QPushButton("5");
button[6] = new QPushButton("6");
button[7] = new QPushButton("7");
button[8] = new QPushButton("8");
button[9] = new QPushButton("9");
button[10] = new QPushButton("=");
button[11] = new QPushButton("+");
button[12] = new QPushButton("-");
button[13] = new QPushButton("*");
button[14] = new QPushButton("/");
button[15] = new QPushButton("CE");
QGridLayout *gLayout = new QGridLayout;
int r ;
int c ;
int n = 0 ;
for (r=0;r<3;r++)
for(c = 0;c < 3 ;c++)
{
gLayout->addWidget(button[n+1],r,c);
connect(button[n+1],SIGNAL(clicked()),this,SLOT(setValue()));
n++;
}
gLayout->addWidget(button[0],3,0);
connect(button[0],SIGNAL(clicked()),this,SLOT(setValue()));
gLayout->addWidget(button[10],3,1);
connect(button[10],SIGNAL(clicked()),this,SLOT(calculate()));
gLayout->addWidget(button[11],3,2);
connect(button[11],SIGNAL(clicked()),this,SLOT(setOperat()));
gLayout->addWidget(button[12],0,3);
connect(button[12],SIGNAL(clicked()),this,SLOT(setOperat()));
gLayout->addWidget(button[13],1,3);
connect(button[13],SIGNAL(clicked()),this,SLOT(setOperat()));
gLayout->addWidget(button[14],2,3);
connect(button[14],SIGNAL(clicked()),this,SLOT(setOperat()));
gLayout->addWidget(button[15],3,3);
connect(button[15],SIGNAL(clicked()),this,SLOT(clear()));
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(edit);
vLayout->addLayout(gLayout);
setLayout(vLayout);
};
void Calculator::setValue()
{
if (operat==4)
{
clear();
}
QString str=edit->text();
str+=((QPushButton *)sender())->text();
if(operat<0)
{
first = str.toDouble();
edit->setText(QString::number(first));
}
else
{
second = str.toDouble();
edit->setText(QString::number(second));
}
}
void Calculator::setOperat()
{
QString str = ((QPushButton *)sender())->text();
edit->setText(0);
if(str=="+")
{
operat=0;
}
else if (str=="-")
{
operat=1;
}
else if (str=="*")
{
operat=2;
}
else
operat=3;
}
void Calculator::calculate()
{
double all;
switch (operat)
{
case 0:all=first+second;edit->setText(QString::number(all));break;
case 1:all=first-second;edit->setText(QString::number(all));break;
case 2:all=first*second;edit->setText(QString::number(all));break;
case3:all=first*1.0/second;edit->setText(QString::number(all));break;
}
operat=4;
first=all;
}
void Calculator::clear()
{
first=0;
second=0;
operat=-1;
edit->setText("0");
}
麻烦帮我把重要的几个环节 注释一下 谢谢各位大虾~~
展开
 我来答
梦想窗外
2009-05-28 · TA获得超过1588个赞
知道大有可为答主
回答量:1180
采纳率:0%
帮助的人:1557万
展开全部
这里的程序很简单,有哪些不明白的地方啊?不要告诉我你想手动编码如何放置和布局控件吧?
自行车123489
2009-05-30 · TA获得超过370个赞
知道答主
回答量:434
采纳率:0%
帮助的人:156万
展开全部
jh
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式