Qt实验一
来源:互联网 发布:数据库的审计类型有 编辑:程序博客网 时间:2024/06/02 06:53
在一个对话框实现账号,密码输入和验证,若正确则跳转到另一个提示登陆成功对话框,若错误则跳转到另一个提示错误并带有返回按键的对话框。
首先在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就得到可执行文件。