QT编程--用户注册程序的设计与实现

一个用户注册的程序,应用QT编程,完成一个界面简洁、可以记录并判断用户注册信息的程序

功能说明
(1)当用户点击注册后,可以将用户名、姓名、邮箱、性别、出生日期、注册日期时间、个人爱好等写入文件。
(2)可以通过遍历文件中已注册的用户信息,判断用户名是否重复。
(3)可以判断输入的邮箱格式是否正确。
(4)可以判断用户名的长度,若用户名为空或过长则注册失败。
(5)可以判断姓名的长度,若用户名为空或过长则注册失败。
(6)对以上的信息通过弹出对话框显示。
需求分析
1。功能需求分析
实现用户信息的写入和读出,弹出对话框显示程序信息。
2。 数据需求分析
实现对不合法的信息的判断,并弹出对话框显示信息。
3。 行为需求分析
当用户点击注册按钮时提交注册信息,当用户点击选择按钮时记录性别和爱好信息。
4。 其他需求
实现交互的友好性。
源代码

1. 将用户信息和注册时间用QString存储
QDateTime datetime;
    QString time_str=datetime.currentDateTime().toString("yyyy-MM-dd HH-mm-ss");
    QString struser     =  ui->Username->text();
    QString strname     =  ui->Name->text();
    QString strmail     =  ui->Mail->text();
    QString strdate     =  ui->Dateofbirth->text();
QString转化为string
    string smail = strmail.toStdString();
    string suser = struser.toStdString();
    string sname = strname.toStdString();
创建文本文件
    QString fileName = "Register.txt";
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly ))
    {
        QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
    }
QTextStream stream(&file);
遍历文件检测用户名
    QString usertemp;
QString user=struser;
while (!stream.atEnd())
    {
        usertemp += stream.readLine().section(":",1,1);
    }
   if(usertemp.indexOf(user)>=0)
    {
        QMessageBox *dig=new QMessageBox();
        dig->setText("注册失败:用户名"+struser+"已经被使用");
        dig->show();
        judgeuser++;

    }
   else judgeuser = 0;
   if(suser.length()>8 && suser.length()==0)
   {
       QMessageBox *dig=new QMessageBox();
       dig->setText("注册失败:用户名不能过长且不能为空");
       dig->show();
       judgeuserlenth++;
   }
   else
       judgeuserlenth = 0;
检测姓名长度
   if(sname.length()>8 && sname.length()==0)
   {
       QMessageBox *dig=new QMessageBox();
       dig->setText("注册失败:姓名不能过长且不能为空");
       dig->show();
       judgeuserlenth++;
   }
   else
       judgeuserlenth = 0;
检测邮箱合法性
   if(IsValidEmail(smail)==false)
   {
       QMessageBox *dig=new QMessageBox();
       dig->setText("注册失败:邮箱格式错误");
       dig->show();
       judgemail++;
   }
   else judgemail = 0;
弹出对话框显示注册成功
    if(judgeuser == 0 && judgemail == 0 && judgeuserlenth == 0)
    {
        QMessageBox *dig=new QMessageBox();
        dig->setText(struser+"用户,您好!恭喜您已经注册成功");
        dig->show();
    }
    file.close();
    //QFile file1(fileName);
    //if(!file1.open(QIODevice::ReadOnly ))
    //{
        //QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
   // }
   // QTextStream stream1(&file1);
将用户信息写入文本文件
    if(judgeuser == 0 && judgemail == 0 && judgeuserlenth == 0)
    {
        if(file.open(QIODevice::WriteOnly  | QIODevice::Text|QIODevice::Append))
    {

        if(ui->Male->isChecked())
          stream<<":"+struser+":"+strname+":male:"+strmail+":"+strdate+":"+time_str;
        else if (ui->Female->isChecked())
          stream<<":"+struser+":"+strname+":female:"+strmail+":"+strdate+":"+time_str;
        if(ui->Book->isChecked())
            stream<<":read";
        if(ui->Ball->isChecked())
            stream<<":ball";
        if(ui->Internet->isChecked())
            stream<<":intertnet";
        if(ui->Chess->isChecked())
            stream<<":chess";
        if(ui->Music->isChecked())
            stream<<":music";
        stream<<"\n";

        file.close();//关闭文件
}

判断邮箱格式的具体实现
bool MainWindow::IsValidChar(char ch)
{
    if( (ch>=97) && (ch<=122) ) //26个小写字母
        return true;
    if( (ch>=65) && (ch<=90) ) //26个大写字母
        return true;
    if((ch>=48) && (ch<=57)) //0~9
        return true;
    if( ch==95 || ch==45 || ch==46 || ch==64 ) //_-.@
        return true;
    return false;
}
bool MainWindow::IsValidEmail(string strEmail)
{
    if( strEmail.length()<5 ) //26个小写字母
    return false;

    char ch = strEmail[0];
    if( ((ch>=97) && (ch<=122)) || ((ch>=65) && (ch<=90))  || ((ch>=48) && (ch<=57)))
    {
        int atCount =0;
        int atPos = 0;
        int i;
        int dotCount = 0;
        for(i=1;i<strEmail.length();i++) //0已经判断过了,从1开始
        {
            ch = strEmail[i];
            if(IsValidChar(ch))
            {
                if(ch==64) //"@"
                {
                    atCount ++;
                    atPos = i;
                }
                else if( (atCount>0) && (ch==46) )//@符号后的"."号
                    dotCount ++;
            }
            else
                return false;
        }
        //6. 结尾不得是字符“@”或者“.”
        if( ch == 46 )
            return false;
        //2. 必须包含一个并且只有一个符号“@”
        //3. @后必须包含至少一个至多三个符号“.”
        if( (atCount!=1) || (dotCount<1) || (dotCount>3) )
            return false;
             //5. 不允许出现“@.”或者.@
        int x,y;
        x=strEmail.find("@.");
        y=strEmail.find(".@");
        if( x>0 || y>0 )
        {
            return false;
        }
        return true;
    }
   return false;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值