根据以上所述是可以进行登录,后台数据库验证了,我们需要记住密码等一系列操作,方便用户登录,使用对Xml文件的查询和删除进行实现,login.xml文件内容如下所示
<?xml version='1.0' encoding='UTF-8'?>
<data>
<user id="1">
<username>1305120114</username>
<password>1305120114</password>
<remember>1</remember>
<autologin>0</autologin>
</user>
</data>对其进行查询,然后写到我们的控件中,在loginDialog.h的头文件类中写入下列成员
private slots:void on_comboBoxUserName_activated(const QString &arg1); //用户名点击槽函数void on_comboBoxUserName_currentTextChanged(const QString &arg1);//用户名改变槽函数void on_checkBoxAuto_clicked(); //自动登录点击槽函数void on_checkBoxRemeber_clicked(); //记住密码点击槽函数
private:
QList<QString>listStringUsnm; //用来存储用户名
QList<QString>listStringPsd; //用来存储密码
QList<QString>listStringRemember;//用来存储是否密码
QList<QString>listStringAuto; //用来存储是否自动登录
QList<QString>listStringID; //用来存储属性
protected:
void writeXml(); //写xml文件
bool readXml(); //读取xml文件
void updateXml(QString id, QString rem, QString aut); //更新xml文件然后在LoginDialog.cpp的实现成员函数
/***************写xml文件*****************/
void LoginDialog::writeXml()
{
QFile file("login.xml");
QDomDocument dom;
if(file.open(QIODevice::ReadOnly))
{
if(!dom.setContent(&file)) //将该xml文件导入到dom中
{
file.close();
return ;
}
}
else
{
return ;
}
file.close();
//创建一个新的用户
QDomElement root=dom.documentElement();
QDomElement user=dom.createElement("user");
QDomAttr id=dom.createAttribute("id");
QDomElement usernm=dom.createElement("username");
QDomElement passwd=dom.createElement("password");
QDomElement remember=dom.createElement("remember");
QDomElement autoLogin=dom.createElement("autologin");
QDomText text;
QString num=root.lastChild().toElement().attribute("id"); //获取xml文件最后一个id的大小是多少
int count=num.toInt()+1;
id.setValue(QString::number(count)); //将id+1后添加到新建的用户中
user.setAttributeNode(id);
//将从界面获取的用户名和密码,加到xml文件中
text=dom.createTextNode(ui->comboBoxUserName->currentText());
usernm.appendChild(text);
text=dom.createTextNode(ui->lineEditPassWord->text());
passwd.appendChild(text);
if(ui->checkBoxRemeber->isChecked())
{
text=dom.createTextNode("1");
remember.appendChild(text);
}
else
{
text=dom.createTextNode("0");
remember.appendChild(text);
}
if(ui->checkBoxAuto->isChecked())
{
text=dom.createTextNode("1");
autoLogin.appendChild(text);
}
else
{
text=dom.createTextNode("0");
autoLogin.appendChild(text);
}
//将所有节点依次对应写到根节点的目录下
user.appendChild(usernm);
user.appendChild(passwd);
user.appendChild(remember);
user.appendChild(autoLogin);
root.appendChild(user);
QFile f("login.xml");
if(!f.open(QIODevice::WriteOnly|QIODevice::Truncate))
{
return;
}
QTextStream out(&f);
dom.save(out,4);
f.close();
}
/***************读取xml文件***************/
bool LoginDialog::readXml()
{
int Count=0;
listStringPsd.clear(); //清空list存储
listStringUsnm.clear(); //清空list存储
listStringAuto.clear(); //清空list存储
listStringRemember.clear();//清空list存储
listStringID.clear(); //清空list存储
QDomDocument dom;
QFile *file=new QFile("login.xml");
if(file->open(QIODevice::ReadOnly))
{
if(!dom.setContent(file)) //将该xml文件导入到dom中
{
file->close();
return false;
}
}
else
{
return false;
}
file->close();
QDomElement docElem=dom.documentElement(); //返回根元素
QDomNode node=docElem.firstChild(); //返回根节点的第一个子节点
while(!node.isNull()) //如果节点不为空
{
if(node.isElement()) //如果节点是元素
{
QDomElement element=node.toElement();//将其转化为元素
listStringID<<element.attribute("id");
QDomNodeList list=element.childNodes();//取出该元素的所有子节点放到list中
//将子节点元素全部取出来
for(int i=0;i<list.count();i++)
{
QDomNode domNode=list.at(i);
if(domNode.isElement())
{
//取出我们所要的数据
switch(i)
{
case 0:ui->comboBoxUserName->addItem(domNode.toElement().text());listStringUsnm<<domNode.toElement().text();break;
case 1:listStringPsd<<domNode.toElement().text();break;
case 2:listStringRemember<<domNode.toElement().text();break;
case 3:listStringAuto<<domNode.toElement().text();break;
}
}
}
}
Count++;
node=node.nextSibling(); //下一个兄弟节点
}
if(Count>0)
{
return true;
}
else
{
return false;
}
}
/***************更新xml文件***************/
void LoginDialog::updateXml(QString id,QString rem,QString aut)
{
qDebug()<<"updata";
QDomDocument dom;
QFile *file=new QFile("login.xml");
if(file->open(QIODevice::ReadOnly))
{
if(!dom.setContent(file)) //将该xml文件导入到dom中
{
file->close();
return ;
}
}
else
{
return ;
}
file->close();
QDomNodeList nodeList=dom.elementsByTagName("user"); //返回根元素
//以标签名进行查找
for(int i=0;i<nodeList.count();i++)
{
QDomElement e=nodeList.at(i).toElement();
if(e.attribute("id")==id)
{
QDomNodeList child=nodeList.at(i).childNodes(); //找到它的所有子节点
child.at(2).toElement().firstChild().setNodeValue(rem);//将子节点的内容更新
child.at(3).toElement().firstChild().setNodeValue(aut);//将子节点的内容更新
}
}
QFile f("login.xml");
if(!f.open(QIODevice::WriteOnly|QIODevice::Truncate))
{
return;
}
QTextStream out(&f);
dom.save(out,4);
f.close();
}
/***************删除xml节点***************/
bool LoginDialog::deletetXml()
{
}
/***************点击列表框*****************/
void LoginDialog::on_comboBoxUserName_activated(const QString &arg1)
{
int Count=0;
int tempCount=0;
for(int i=0;i<ui->comboBoxUserName->count();i++)
{
//将listFloat存储的信息读取出来,进行实时改变lineEdit的密码
if(ui->comboBoxUserName->currentText()==listStringUsnm[i]&&listStringRemember[i]=="1")
{
Count++;
ui->checkBoxRemeber->setChecked(true);
ui->lineEditPassWord->setText(listStringPsd[i]);
if(listStringAuto[i]=="1")
{
ui->checkBoxAuto->setChecked(true);
tempCount++;
}
}
}
if(Count==0)
{
ui->checkBoxRemeber->setChecked(false);
ui->lineEditPassWord->clear();
}
if(tempCount==0)
{
ui->checkBoxAuto->setChecked(false);
}
}
/***************列表框文本改变*************/
void LoginDialog::on_comboBoxUserName_currentTextChanged(const QString &arg1)
{
int Count=0;
//判断出如果不是已经在此登录过的密码,则清空
for(int j=0;j<listStringUsnm.count();j++)
{
if(ui->comboBoxUserName->currentText()==listStringUsnm[j])
{
Count++;
}
}
if(Count==0)
{
ui->lineEditPassWord->clear();
}
}
/***************自动登陆******************/
void LoginDialog::on_checkBoxAuto_clicked()
{
if(!ui->checkBoxRemeber->isChecked())
{
ui->checkBoxAuto->setChecked(false);
}
}
/***************记住密码******************/
void LoginDialog::on_checkBoxRemeber_clicked()
{
if(!ui->checkBoxRemeber->isChecked())
{
ui->checkBoxAuto->setChecked(false);
}
}将登录按钮的响应函数进行修改,改变为如下代码
/***************登录按钮********************/
void LoginDialog::on_pushButtonLogin_clicked()
{
int Count=0;
QString tempRem,tempAuto;
if(login->dataSelect(ui->comboBoxUserName->currentText(),ui->lineEditPassWord->text()))
{
//判断出输入的用户名是否存在,如果存在则不写入xml中,如果不存在则写入xml中
for(int i=0;i<listStringUsnm.count();i++)
{
if(ui->comboBoxUserName->currentText()==listStringUsnm[i]&&ui->lineEditPassWord->text()==listStringPsd[i])
{
Count++;
if(ui->checkBoxRemeber->isChecked())
{
tempRem="1";
}
else
{
tempRem="0";
}
if(ui->checkBoxAuto->isChecked())
{
tempAuto="1";
}
else
{
tempAuto="0";
}
if(tempRem!=listStringRemember[i]||tempAuto!=listStringAuto[i])//判断其自动登录和记住密码是否改变
{
updateXml(listStringID[i],tempRem,tempAuto); //更新xml文件
}
}
}
if(Count==0)
{
writeXml();
}
accept(); //判断登录成功后accept,在main.cpp中进入主窗口
}
else
{
QMessageBox::information(this,tr("系统提示"),tr("登录失败"));//登录失败
}
}到此,整个Demo就算完成要想下载源码,请点击链接http://download.csdn.net/detail/yinyuchen1/9625567