练习:
登录界面增加注册功能(在本地增加用户文件进行比对用户)
LoginWindow.h
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QMainWindow>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#include <QMessageBox>
#include <QFile>
class LoginWindow : public QMainWindow
{
Q_OBJECT
signals:
void pushToHomeWindow();
public slots:
void loginButtonEvent();
void registerEvent();
public:
LoginWindow(QWidget *parent = 0);
~LoginWindow();
private:
QLineEdit *usernameInput;
QLineEdit *passwdInput;
};
#endif // LOGINWINDOW_H
LoginWindow.cpp
#include "loginwindow.h"
const QString USER_TABLE_PATH = "./userTable.txt";
void showAlert(QString msg) {
QMessageBox::information(nullptr, "提醒", msg);
}
LoginWindow::LoginWindow(QWidget *parent)
: QMainWindow(parent)
{
this->resize(600, 800);
this->setWindowIcon(QIcon(":/images/QQ.png"));
this->setWindowTitle("登录界面");
this->setStyleSheet("background-color:white");
int offsetX = (this->width()-480)/2, offsetY = 10;
//LOGO
QLabel *logoLabel = new QLabel(this);
logoLabel->setPixmap(QPixmap(":/images/logo.png"));
logoLabel->setGeometry(offsetX, offsetY, 480, 266);
offsetY += (266 + 20);
//登录框
offsetX = (this->width()-260)/2;
QLabel *loginIcon = new QLabel(this);
loginIcon->setPixmap(QPixmap(":/images/userName.jpg"));
loginIcon->setScaledContents(true);
loginIcon->setGeometry(offsetX, offsetY, 50, 50);
offsetX += (50+10);
QLineEdit *usernameInput = new QLineEdit(this);
this->usernameInput = usernameInput;
usernameInput->setPlaceholderText("请输入用户名");
usernameInput->setGeometry(offsetX, offsetY, 200, 50);
usernameInput->setStyleSheet("border:none");
QWidget *loginBottomLine = new QWidget(this);
loginBottomLine->setStyleSheet("background-color:black");
loginBottomLine->setGeometry(usernameInput->x(), usernameInput->y()+usernameInput->height(), usernameInput->width(), 1);
offsetY += (50+10);
offsetX = (this->width()-260)/2;
QLabel *passwdIcon = new QLabel(this);
passwdIcon->setPixmap(QPixmap(":/images/passwd.jpg"));
passwdIcon->setScaledContents(true);
passwdIcon->setGeometry(offsetX, offsetY, 50, 50);
offsetX += (50+10);
QLineEdit *passwdInput = new QLineEdit(this);
this->passwdInput = passwdInput;
passwdInput->setPlaceholderText("请输入密码");
passwdInput->setGeometry(offsetX, offsetY, 200, 50);
passwdInput->setEchoMode(QLineEdit::Password);
passwdInput->setStyleSheet("border:none");
QWidget *passwdBottomLine = new QWidget(this);
passwdBottomLine->setStyleSheet("background-color:black");
passwdBottomLine->setGeometry(passwdInput->x(), passwdInput->y()+passwdInput->height(), passwdInput->width(), 1);
offsetY += 60;
//登录按钮
offsetX = (this->width()-150)/2;
QPushButton *loginBtn = new QPushButton(this);
loginBtn->setIcon(QIcon(":/images/login.png"));
loginBtn->setText("登录");
loginBtn->setIconSize(QSize(20, 20));
loginBtn->setGeometry(offsetX, offsetY, 80, 44);
connect(loginBtn, &QPushButton::clicked, this, &LoginWindow::loginButtonEvent);
offsetX += (80 + 20);
//取消按钮
QPushButton *cancelBtn = new QPushButton(this);
cancelBtn->setIcon(QIcon(":/images/cancel.png"));
cancelBtn->setText("取消");
cancelBtn->setIconSize(QSize(20, 20));
cancelBtn->setGeometry(offsetX, offsetY, 80, 44);
connect(cancelBtn, &QPushButton::clicked, [=]() {
//取消按钮点击事件
int res = QMessageBox::question(this, "提醒", "是否要退出登录", QMessageBox::Yes | QMessageBox::No);
if (res == QMessageBox::Yes) {
this->close();
}
});
offsetX += (80 + 20);
//注册
QPushButton *registerBtn = new QPushButton(this);
registerBtn->setText("注册");
registerBtn->setGeometry(offsetX, offsetY, 80, 44);
connect(registerBtn, &QPushButton::clicked, this, &LoginWindow::registerEvent);
offsetY += 70;
this->resize(this->width(), offsetY);
}
bool checkUser(QString username, QString passwd) {
QFile file(USER_TABLE_PATH);
if (!file.exists()) {
showAlert("登录失败");
return false;
}
if (!file.open(QFile::ReadOnly)) {
showAlert("打开用户表失败");
return false;
}
while(!file.atEnd()) {
QByteArray byteArray = file.readLine();
QString orgStr = QString(byteArray);
QStringList list = orgStr.split(" ");
QString tempUsername = list[0];
QString tempPasswd = list[1];
tempPasswd.remove(tempPasswd.size()-1, 1);
if (tempUsername == username && tempPasswd == passwd) {
return true;
}
}
return false;
}
void LoginWindow::loginButtonEvent() {
//登录事件
if (checkUser(this->usernameInput->text(), this->passwdInput->text())) {
QMessageBox box(QMessageBox::Information, "提醒", "登录成功", QMessageBox::Ok, this);
//box.button(QMessageBox::Ok)->setText("aaa");
int res = box.exec();
if (res == QMessageBox::Ok) {
qDebug() << "登录成功";
emit pushToHomeWindow();
this->close();
}
} else {
int res = QMessageBox::warning(this, "提醒", "账号密码不匹配,是否重新登录?", QMessageBox::Ok | QMessageBox::Cancel);
if (res == QMessageBox::Ok) {
this->passwdInput->setText("");
} else if (res == QMessageBox::Cancel) {
this->close();
}
}
}
//注册事件
void LoginWindow::registerEvent() {
if (this->usernameInput->text().length() <= 0 || this->passwdInput->text().length() <= 0) {
showAlert("请输入用户名和密码");
return;
}
QFile file(USER_TABLE_PATH);
QString username = this->usernameInput->text();
QString passwd = this->passwdInput->text();
QString res = username + " " + passwd + "\n";
if (!file.open(QFileDevice::Append)) {
showAlert("打开文件失败");
return;
}
file.write(res.toLocal8Bit().data());
showAlert("注册成功");
}
LoginWindow::~LoginWindow()
{
}
结果展示: