QT calculator step by step

Qt计算机的制作 

tutorial: https://www.youtube.com/watch?v=txGRU7OrTZo&t=123s

老师的源码:https://www.newthinktank.com/2018/06/qt-tutorial-2-c-calculator/

请结合视频,老师的源码观看!本文只做个人学习记录(特别是错误)。

首先,拖拽一个lineEdit(不是TextEdit是LineEdit)!!!!TextEdit不能预设文本内容,但是LineEdit可以。如果是一行文字输入请用LineEdit.

 

整体布局改为grid  

change horizontal to preferred and change vertical to expanding

change lineEdit name to Display. and delete toolbar

2)add a lot of bottoms and drag the Display area into full screen

 set all pushbottom vertical and horizontal to preferred. styleSheet: This is sth. like the css. The color C0C0C0 can be found in wiki web color graph. 

 注意这里 QLineEdit和QPushButton的区别,学会了后可轻松运用

edit the main display area. change the stylesheet as above and change the font to aria bond 36 sth. like this. change alignment to align to the right. =》见上图 

change the pushbottom display as below:

change the name of the pushBtms like below: button1- button9 

 

ui->Display->setText(//string);//非常常见的命令,用来命令lineEdit内应该显示的文字。例如: 

ui->Display->setText(QString::number(calcVal));
//QString::number() 把数字转化为string

change equal botton to EqualBottonPressed();

 有用代码: how to get the text on the bottom:

QString butval = botton->text(); //得到pushBotton上的文字
//这里的botton是QPushBottom *botton; 是一个QPushBottom的pointer
//可以用任意名词代替。

 string to double 代码:

string_name.toDouble();
//eg. string_name.toDouble() == 0;

注意:LineEdit里的数字 其实是string!随时记得转化为string_name.toDouble();

以及要把计算的数字展现到lineEdit是需要转化为string再放入ui->lineEdit->setText(string);

number to string 代码:

ui->Display->setText(QString::number(solution));//solution 为double object

 change butval to butVal; NumPressed function 显示数字,保留数字信息

EqualBotton

代码解析: 判断计算机上的数字符号

if(QString::compare(butVal, "/", QT::CaseInsensitive) == 0){
    divTrigger = true;
}//That is how you handle math bottom

3)定义MathButtonPressed()

 4)定义EqualBottonPressed() //注意每个function前的Calculation:: 

 

 5) ChangNumber:

HomeWork: doing clear and memory function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值