QT登陆窗口代码

这篇博客展示了如何使用QT创建一个登录窗口,并实现用户输入验证功能。通过对话框(Dialog)类,设置用户名(user)和密码(pass)输入框为密码模式,限制输入长度,并在点击登录按钮后,连接到MySQL数据库进行用户名和密码的匹配验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "dialog.h"
#include "ui_dialog.h"
#include "QtGui"
#include "mysql/mysql.h"
#include<cstdio>
#include<cstring>
#include<string>

using namespace std;
Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->pass->setEchoMode(QLineEdit::Password);
    this->setFixedSize(this->width(),this->height());
    ui->user->setFocus();
    ui->user->setMaxLength(16);
    //ui->user->setMaximumWidth(16);
    //ui->pass->setMaximumWidth(16);
    ui->pass->setMaxLength(16);
}

Dialog::~Dialog()
{
    delete ui;
}


bool Dialog::judge(QString a,QString b,bool &flag)
{
    MYSQL *con=mysql_init(NULL);
    MYSQL_RES *res=NULL;
    MYSQL_ROW row;
    string order("");
    order+="select pass from userdata where user='";
    order+=a.toStdString().c_str();
    order+="'";

    bool iserror;
    if(!con)
    {
        flag=true;
        QMessageBox::warning(this,tr("INIT ERROR!"),tr(mysql_error(con)),QMessageBox::Yes);
        return false;
    }
    con=mysql_real_connect(con,"127.0.0.1","root","root","qt",0,NULL,0);
    if(!con)
    {
        flag=true;
        QMessageBox::warning(this,tr("CONNECT ERROR!"),tr("Please check network!"),QMessageBox::Yes);
        return false;
    }
    iserror=mysql_query(con,order.c_str());
    if(iserror)
    {
        flag=true;
        QMessageBox::warning(this,tr("QUERY ERROR!"),tr(mysql_error(con)),QMessageBox::Yes);
        return false;
    }
    else
    {
        res=mysql_use_result(con);

        if(res)
        {
            if((row=mysql_fetch_row(res))!=0)
                if(b.toStdString()==string(row[0]))return true;
        }
    }
    return false;
}



void Dialog::on_login_clicked()
{
    bool flag=false;
    if(ui->user->text().isEmpty()||ui->pass->text().isEmpty())
    {
        QMessageBox:: warning(this,tr("ERROR"),tr("Username or Password is empty!"),QMessageBox::Yes);
        if(ui->user->text().isEmpty()||(ui->user->text().isEmpty()&&ui->pass->text().isEmpty()))ui->user->setFocus();
        else if(ui->pass->text().isEmpty())ui->pass->setFocus();
    }
    else if(judge(ui->user->text(),ui->pass->text(),flag))
    {
        accept();
    }
    else
    {
        if(!flag)
        QMessageBox::warning(this,tr("EROOR"),tr("Username or Passwd Error!"),QMessageBox::Yes);
        ui->user->clear();
        ui->pass->clear();
        ui->user->setFocus();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值