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