Qt实验一

在一个对话框实现账号,密码输入和验证,若正确则跳转到另一个提示登陆成功对话框,若错误则跳转到另一个提示错误并带有返回按键的对话框。

 

首先在Qt Designer中新建一个工程。

 

新建三个对话框,在里面添加各种窗口元件,按钮,行输入框等。结果如下:

 

第一个为初始界面:

 

第二个为显示输入正确界面:

 

第三个为显示输入错误界面:

 

 

对每个按钮进行信号槽的实现:

 

 

 

双击第一个对话框(Form3),对新建的槽函数进行实现:

 

#include "Form4.h"

#include "Form5.h"

 

void Form3::showOne() //显示1

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){ //假如焦点在第一个行中

str = lineEdit1->text(); //获得第一行当前字符串赋值在str

str.append("1"); //给这个字符串str追加"1"

lineEdit1->setText(str); //把str重新写入行中

    }

    else if(lineEdit2->hasFocus() == TRUE){ //假如焦点在第二行中

str = lineEdit2->text();

str.append("1");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showTwo()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("2");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("2");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showThree()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("3");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("3");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showFour()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("4");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("4");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showFive()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("5");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("5");

lineEdit2->setText(str);

    }

}

 

void Form3::showSix()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("6");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("6");

lineEdit2->setText(str);

    }

}

 

void Form3::showSeven()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("7");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("7");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showEight()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("8");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("8");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showNine()

{

    QString str;

 

     if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("9");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("9");

lineEdit2->setText(str);

    }

}

 

 

void Form3::showZero()

{

    QString str;

 

    if(lineEdit1->hasFocus() == TRUE){

str = lineEdit1->text();

str.append("0");

lineEdit1->setText(str);

    }

    else if(lineEdit2->hasFocus() == TRUE){

str = lineEdit2->text();

str.append("0");

lineEdit2->setText(str);

    }

}

 

 

 

void Form3::login() //登录,检察帐号密码

{

    QString str1, str2;

 

    str1 = lineEdit1->text();

    str2 = lineEdit2->text();

    //判断帐号密码是否与预设值相同

    if(str1 == "123" && str2 == "000"){ //若正确,跳转到提示正确的对话框中

Form4 w; //定义Form4对象w

this->close(); //把当前对话框关闭

w.show(); //w显示

w.exec(); //把控制权交给w

    }

    else{ //若不正确,跳转到提示错误的对话框中

Form5 w2;

this->close();

w2.show();

w2.exec();

    }

 

}

 

 

 

在提示错误的对话框(Form5),中再对按钮(返回)实现信号槽:

 

函数实现:

 

#include "Form3.h"

 

void Form5::returnForm3()

{

    Form3 w;

 

    this->close();

    w.show();

    w.exec();

}

 

最后新建Main-File文件,qt会自动生成必要代码。这样就可以了。

 

在经过,qmake和make就得到可执行文件。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值