###实现效果,勾选记住密码获取lineEdit的值并保存
##源程序
##mymainwindow.cpp
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QString>
#include <QMessageBox>
#include <QSettings>
myMainWindow::myMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::myMainWindow)
{
ui->setupUi(this);
QSettings setting("C:/Window/config.ini",QSettings::IniFormat);//读配置文件
str_3=setting.value("Login/account").toString();
ui->lineEdit->setText(str_3);
str_4=setting.value("Login/password").toString();
ui->lineEdit_2->setText(str_4);
ui->checkBox->setChecked(true);
}
myMainWindow::~myMainWindow()
{
delete ui;
}
void myMainWindow::on_lineEdit_2_textChanged(const QString &arg1)
{
ui->lineEdit_2->setEchoMode(QLineEdit::Password);
}
void myMainWindow::on_pushButton_clicked()
{
str_1 = ui->lineEdit->text();
str_2 = ui->lineEdit_2->text();
QSettings setting("C:/Window/config.ini",QSettings::IniFormat);//读配置文件
if(str_1 == "" || str_2 == "")
{
QMessageBox::information(this, "错误!", "用户名或者密码不能为空!", QMessageBox::Ok);
return;
}
else if(str_1==str_3&&str_2==str_4)
{
close();
}
else if(str_1==str_3&&str_2!=str_4)
{
QMessageBox::information(this, "错误!", "您输入的密码有误!", QMessageBox::Ok);
return;
}
void on_checkBox_clicked();
}
void myMainWindow::on_pushButton_2_clicked()
{
close();
}
void myMainWindow::on_checkBox_clicked()
{
}
void myMainWindow::on_checkBox_stateChanged(int arg1)
{
if(arg1=ui->checkBox->isChecked())
{
remeberPasswd=true;
str_1 = ui->lineEdit->text();
str_2 = ui->lineEdit_2->text();
QSettings setting("C:/Window/config.ini",QSettings::IniFormat);//读配置文件
setting.beginGroup(tr("Login"));
setting.setValue("account",str_1);//设置key和value,也就是参数和值
setting.setValue("password",str_2);
setting.setValue("remeber",true);
setting.endGroup();//节点结束
}
else
{
ui->lineEdit_2->clear();
remeberPasswd=false;
}
}
##//main.cpp
#include "mymainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myMainWindow w;
w.show();
return a.exec();
}
##//mymainwindow.h
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class myMainWindow;
}
class myMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit myMainWindow(QWidget *parent = 0);
~myMainWindow();
QString str_1,str_2,str_3,str_4;
QString RemeberPasswd;
bool remeberPasswd,r;
private slots:
void on_lineEdit_2_textChanged(const QString &arg1);
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_checkBox_clicked();
void on_checkBox_stateChanged(int arg1);
private:
Ui::myMainWindow *ui;
};
#endif // MYMAINWINDOW_H