day2_QT
- 登录窗口
登录窗口
loginwindow.h文章来源:https://uudwc.com/A/oL3AJ
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QWidget>
#include <QDebug>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QObject>
#include "homepage.h"
class LoginWindow : public QWidget
{
Q_OBJECT
private slots:
void get_accountAndpwd();
public:
LoginWindow(QWidget *parent = nullptr);
~LoginWindow();
private:
QPushButton *login;
QPushButton *exit;
QLineEdit *editpwd;
QLabel *labelpwd;
QLineEdit *editaccount;
QLabel *labelaccount;
QLabel *labelpic;
HomePage *homepage;
};
#endif // LOGINWINDOW_H
loginwindow.c文章来源地址https://uudwc.com/A/oL3AJ
#include "loginwindow.h"
#include <QMessageBox>
LoginWindow::LoginWindow(QWidget *parent)
: QWidget(parent)
{
//设置窗口标题和图标
this->setWindowTitle("ChatWe");
this->setWindowIcon(QIcon(":/login.png"));
//设置窗口的大小 固定
this->setFixedSize(800,500);
//设置上1/2处label图标放置背景图
this->labelpic = new QLabel(this);
//一半大小
this->labelpic->resize(800,250);
//放入图片
this->labelpic->setPixmap(QPixmap(":/bg.png"));
/***********************************************************************/
//账号图标
this->labelaccount = new QLabel(this);
this->labelaccount->resize(50,50);
//移动位置
this->labelaccount->move(150,280);
this->labelaccount->setPixmap(QPixmap(":/account.png"));
this->labelaccount->setScaledContents(true);
//账号编辑框
this->editaccount = new QLineEdit(this);
this->editaccount->setPlaceholderText("请输入账号");
this->editaccount->setAlignment(Qt::AlignCenter);
this->editaccount->resize(300,50);
this->editaccount->setMaxLength(20);
this->editaccount->move(labelaccount->x()+100,labelaccount->y());
//密码图标
this->labelpwd = new QLabel(this);
this->labelpwd->resize(50,50);
//移动位置
this->labelpwd->move(labelaccount->x(),labelaccount->y()+80);
this->labelpwd->setPixmap(QPixmap(":/psw.png"));
this->labelpwd->setScaledContents(true);
//密码编辑框
this->editpwd = new QLineEdit(this);
this->editpwd->setPlaceholderText("请输入密码");
this->editpwd->setEchoMode(QLineEdit::Password);
this->editpwd->setAlignment(Qt::AlignCenter);
this->editpwd->resize(300,50);
this->editpwd->setMaxLength(20);
this->editpwd->move(editaccount->x(),editaccount->y()+80);
//登录按钮
this->login = new QPushButton(QIcon(":/accept.png"),"登录",this);
this->login->move(editpwd->x()+10,editpwd->y()+80);
//退出按钮
this->exit = new QPushButton(QIcon(":/reject.png"),"退出",this);
this->exit->move(editpwd->x()+180,editpwd->y()+80);
/*QT5版本 将clicked 信号发送给自定义的槽函数判断账号和密码*/
this->connect(this->login,&QPushButton::clicked,this, &LoginWindow::get_accountAndpwd);
}
//功能函数,获取账号和密码
void LoginWindow::get_accountAndpwd()
{
QString username = this->editaccount->text();
QString password = this->editpwd->text();
if(username == "admin" && password == "123456"){
//密码正确
QMessageBox successBox(QMessageBox::Information,"success","登录成功",
QMessageBox::Ok|QMessageBox::Cancel);
successBox.setWindowIcon(QIcon(":/login.png"));
int ret = successBox.exec();
if(ret == QMessageBox::Ok){
//关闭当前页面
this->hide();
//跳转到主页
this->homepage = new HomePage();
homepage->show();
}else {
//弹出询问界面
QMessageBox exitBox(QMessageBox::Question,"quit","是否退出?",
QMessageBox::Yes|QMessageBox::No);
exitBox.setWindowIcon(QIcon(":/login.png"));
int ret = exitBox.exec();
if(ret == QMessageBox::Yes){
//关闭当前页面
this->hide();
//跳转到主页
this->homepage = new HomePage();
homepage->show();
}else{
//退出程序
this->close();
}
}
}else{
//账号密码 不匹配 弹出错误对话框
int ret = QMessageBox::critical(this,tr("ChatWe"),tr("账号密码不匹配.\n""是否重新登录?"),
QMessageBox::Ok|QMessageBox::Cancel, //两个选项
QMessageBox::Ok); //默认选项
if(ret == QMessageBox::Ok){
//清空两个框
this->editaccount->clear();
this->editpwd->clear();
}else {
//退出程序
this->close();
}
}
}
LoginWindow::~LoginWindow()
{
}