QT登陆窗口代码

#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();
    }
}


你可能感兴趣的:(QT登陆窗口代码)