qt笔记总结

基于qt的计算器开发:

Qt简单的来说为C++的一个类库,为C++产生图形界面的一个强大的功能库。

#include<QApplication>//qt头文件引入相关的qt文件

#include<QLabel>//qt中的类,标签。

Int main(int arg,char *ardv[])

{

QApplication a(argc,argv)//实例化一个qt程序

//a.exec();//两行一个qt程序,但窗口为空。

//如下实例化一个窗口

QLabel *label=new OLabel(“hello world”);//实例化QLabel的标签

Label->show();//调用QLabel中的show()函数

return a.exe();//程序进入消息循环,相当于python的mainloop()

}

不依靠qt程序是编译运行的方式

Qmake-project->.pro

Qmake  ->makefile

Make    ->可执行的程序

mainwindow w;          //实例化一个主窗口

w.show(); //显示实例化的窗口

 

QString::append()函数:

具有”+=”的操作符功能

eg:

QString str1=”welcome”;

QString str2=”to”;

str1.append(str2); //str1=”welcome to”

组合字符串函数QString::sprintf():

Eg QString str;

Str.sprintf(“%s”,”welcome”);// str=”welcome”

Str.sprintf(“%s%s”,”welcome”,”to you”);// str=”welcome to you”

另有QString::arg()函数用于字符串的组合

Eg:

QString str;

Str=QString(“%1 was born in %2”).arg(“John”).arg(1982);

//str=”John was born in 1982

其中%1被替换为John %2被替换为1982

Qt Designer //qt设计师,界面设计

pushButton 按钮的功能实现

Label 标检作用,可以在qt设计师内拖拽

Inputwidgets 单行输出文本框

Radio Button 单选按钮

 

Check Button  复选按钮

写程序为qt中称之为消息入槽。

实行信息入槽函数 connect

语法:  connect(谁,发出了什么信号,谁,执行什么操作)

Eg connect(this->ui->btnLogin,SIGNAL);

槽函数:与界面函数绑定的函数:

槽函数的声明 private slots:

qDebug()<<”login”;// 其功能为输出语句

connect实例解释

connect(this->btlogin,SIGNAL(click(bool)),this,SLOT(login()));

当窗口(界面被点击时)用this->ui->btlogin发出接收信息;接收SIGNAL()

消息类型为click(bool) 同时产生反应SLOT(login())调用槽函数login()

注意::qDebug()需要有头文件#include<QDebug>

 

Label

7

8

9

+

<-

4

5

6

-

.

1

2

3

*

1/x

C

0

=

/

 

 

alignment:选右对齐。

新建文件 出现model.h model.cpp

model.h中  计算器需要+,-,*,/,num1,num2;

eg:

private:

int num1;

int num2;

QString flag;

public:// public中设置函数

void setNum1();// int num,参数

void setNum2();// int num,参数

void setflag();// QString flag参数

QString do calc();// 开始计算

model.cpp中代码

void model::setNum1(int num)

{

this->num1=num;

}

void model::setNum2(int num)

{

this->num2=num;

}

void model::setflag(QString flag)

{

this->flag=flag;

}

QString model::do calc()

{

int result=0;

switch(this->flag)

{

case "+":

result=num1+num2;

break;

case "-":

result=num1-num2;

break;

case "*";

result=num1*num2;

break;

case "/":

if(this->num2==0)

return "ERROR";//分母不为0

result=num1/num2;

break;

default:

return QString::number(this->num1);

}

return QString::number(result);

}

构造函数中

model::model()

{

this->num1=0;//构造函数的初始化。

this->num2=0;

}

QString str;

str.setNum(1234);// str="1234" 数字转字符串

long a=63;

QString s=QString::number(a,16/*(进制)*/,);// s=3f

QString t=QString::number(a,16).upper();// t=3F

对于QString model::do calc()的另一种写法

QString model::do calc()

{

int result=0;

if(this->flag=="+")

{

result=this->num1+this->num2;

}

else if(this->flag=="-")

{

result=this->num1-this->num2;

}

else if(tis->flag=="*")

{

result=this->num1*this->num2;

}

else if(this->flag=="/")

{

if(this->num2==0)

return "ERROR";

return=this->num1/this->num2;

}

else

{

result QString::number(this->num1);

}

return QString::number(result);

}

将代码与ui界面相连接

connect(this->ui->btn_0,SIGNAL(click(bool)),this,SLOT(getBtno()));

 

 

在clac.h中的public 中声明按钮函数

private slots:

void getBtno();

void getBtn1();

void getBtn2();

    void getBtn3();

    void getBtn4();

    void getBtn5();

    void getBtn6();

    void getBtn7();

    void getBtn8();

    void getBtn9();

    void on_BC_clicked();

    void on_Badd_clicked();

    void on_dengyu_clicked();

    void on_jian_clicked();

    void on_cheng_clicked();

    void on_chushu_clicked();

    void on_dian_clicked();

    void on_daoshu_clicked();

    void on_pushButton_16_clicked();

    void on_Backspace_clicked();

    void on_bao_clicked();

//在对应的calc.cpp中实现相应的代码函数

calc.cpp

#include "calc.h"
#include "ui_calc.h"
#include<math.h>

calc::calc(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::calc)
{
    ui->setupUi(this);
    this->tmp="";
    this->mode=new model;
    connect(this->ui->B0,SIGNAL(clicked(bool)),this,SLOT(getBtn0()));
    connect(this->ui->B1,SIGNAL(clicked(bool)),this,SLOT(getBtn1()));
    connect(this->ui->B2,SIGNAL(clicked(bool)),this,SLOT(getBtn2()));
    connect(this->ui->B3,SIGNAL(clicked(bool)),this,SLOT(getBtn3()));
    connect(this->ui->B4,SIGNAL(clicked(bool)),this,SLOT(getBtn4()));
    connect(this->ui->B5,SIGNAL(clicked(bool)),this,SLOT(getBtn5()));
    connect(this->ui->B6,SIGNAL(clicked(bool)),this,SLOT(getBtn6()));
    connect(this->ui->B7,SIGNAL(clicked(bool)),this,SLOT(getBtn7()));
    connect(this->ui->B8,SIGNAL(clicked(bool)),this,SLOT(getBtn8()));
    connect(this->ui->B9,SIGNAL(clicked(bool)),this,SLOT(getBtn9()));
}
calc::~calc()
{
    delete ui;
}
void calc::getBtn0()
{
    if(this->tmp!="")
    {
        this->tmp+=this->ui->B0->text();
        this->ui->label->setText(this->tmp);
    }
}
void calc::getBtn1()
{
    this->tmp+=this->ui->B1->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn2()
{
    this->tmp+=this->ui->B2->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn3()
{
    this->tmp+=this->ui->B3->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn4()
{
    this->tmp+=this->ui->B4->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn5()
{
    this->tmp+=this->ui->B5->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn6()
{
    this->tmp+=this->ui->B6->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn7()
{
    this->tmp+=this->ui->B7->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn8()
{
    this->tmp+=this->ui->B8->text();
    this->ui->label->setText(this->tmp);
}
void calc::getBtn9()
{
    this->tmp+=this->ui->B9->text();
    this->ui->label->setText(this->tmp);
}

void calc::on_BC_clicked()
{

    this->tmp="";
    this->ui->label->setText("0");

}
void calc::on_Badd_clicked()
{
    double num1=this->tmp.toDouble();
    this->mode->setNum1(num1);
    this->tmp="";
    this->ui->label->setText("0");
    QString ex=this->ui->Badd->text();
    this->mode->setFlag(ex);
}

void calc::on_dengyu_clicked()
{
    double num=this->tmp.toDouble();
    this->mode->setNum2(num);
    QString res = this->mode->docalc();
    this->ui->label->setText(res);
    this->tmp=res;
}

void calc::on_jian_clicked()
{
    double num1=this->tmp.toDouble();
    this->mode->setNum1(num1);
    this->tmp="";
    this->ui->label->setText("0");
    QString ex=this->ui->jian->text();
    this->mode->setFlag(ex);
}

void calc::on_cheng_clicked()
{
    double num1=this->tmp.toDouble();
    this->mode->setNum1(num1);
    this->tmp="";
    this->ui->label->setText("0");
    QString ex=this->ui->cheng->text();
    this->mode->setFlag(ex);
}

void calc::on_chushu_clicked()
{
    double num1=this->tmp.toDouble();
    this->mode->setNum1(num1);
    this->tmp="";
    this->ui->label->setText("0");
    QString ex=this->ui->chushu->text();
    this->mode->setFlag(ex);
}
void calc::on_dian_clicked()
{
    this->tmp+=this->ui->dian->text();
    this->ui->label->setText(this->tmp);
}

void calc::on_daoshu_clicked()
{
    double num1=1/(this->tmp.toDouble());
    this->tmp=QString::number(num1);
    this->ui->label->setText(this->tmp);
}

void calc::on_pushButton_16_clicked()
{
    double num1=this->tmp.toDouble();
    num1=sqrt(num1);
    this->tmp=QString::number(num1);
    this->ui->label->setText(this->tmp);
}

void calc::on_Backspace_clicked()
{
   this->tmp=this->tmp.left(this->tmp.length()-1);
   this->ui->label->setText(this->tmp);
}

void calc::on_bao_clicked()
{
    this->tmp="";
    this->tmp="鲍禹辰";
    this->ui->label->setText(this->tmp);
}
等号“=”:
{
	double num=this->tmp.toDouble;
	QString res=this->model->do calc();
	this->ui->label->setText(res);
	this->tmp="";
}
C符号:
this->tmp="";
this->ui->label->setText("0");


qLn(3)//	ln3
qPow(x,y)//x 的y次方
qExp(y)//qLn()的逆反

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欲游山河十万里

你的鼓励是我们创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值