login.h
#ifndef LOGIN_H
#define LOGIN_H
#include "form.h"
#include <QMainWindow>
#include <QPushButton>
#include<QDebug>
#include<QMessageBox>
#include<cstring>
#include<QLineEdit>//行编辑器的头文件
#include<QLabel>//标签类的头文件
namespace Ui {
class login;
}
class login : public QMainWindow
{
Q_OBJECT
private:
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *edit1;
QLineEdit *edit2;
signals:
void login_success();
public slots:
void btn1_slot();
void btn2_slot();
public:
explicit login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
};
#endif // LOGIN_H
login.cpp
#include "login.h"
#include "ui_login.h"
#include "form.h"
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
ui->setupUi(this);
this->setFixedSize(600,500);
//图标名
this->setWindowTitle("Tomato");
this->setWindowIcon(QIcon("G:\\study what\\tomato.png"));
//头图
lab1 = new QLabel(this);
lab1->setScaledContents(true);
lab1->setPixmap(QPixmap("G:\\study what\\bg.png"));
lab1->resize(600,300);
//join exit
btn1 = new QPushButton(QIcon("G:\\study what\\tomato.png"),"join",this);
btn1->resize(80,40);
btn2 = new QPushButton(QIcon("G:\\study what\\tomato.png"),"exit",this);
btn2->resize(btn1->size());
btn1->move(350,450);
btn2->move(450,450);
//登录账号图标
lab2 = new QLabel(this);
lab2->setScaledContents(true);
lab2->setPixmap(QPixmap("G:\\study what\\tomato.png"));
lab2->resize(30,30);
lab2->move(200,320);
//登录密码图标
lab3 = new QLabel(this);
lab3->setScaledContents(true);
lab3->setPixmap(QPixmap("G:\\study what\\tomato.png"));
lab3->resize(30,30);
lab3->move(200,370);
//登录文本框
edit1 = new QLineEdit(this);
edit1->resize(200,30);
edit1->move(250,320);
//密码文本框
edit2 = new QLineEdit(this);
edit2->resize(200,30);
edit2->move(250,370);
edit2->setEchoMode(QLineEdit::Password);
connect(btn1,&QPushButton::clicked,this,&login::btn1_slot);
connect(btn2,&QPushButton::clicked,this,&login::btn2_slot);
}
login::~login()
{
delete ui;
}
void login::btn1_slot()
{
QMessageBox msgbox;
int ret;
if(edit1->text() == "admin" && edit2->text() == "123456")
{
msgbox.setIcon(QMessageBox::NoIcon);
msgbox.setWindowTitle("Tomato");
msgbox.setText("login success!!!");
msgbox.setStandardButtons(QMessageBox::Ok);
msgbox.setDefaultButton(QMessageBox::Ok);
msgbox.exec();
emit login_success();
this->close();
}
else if(edit1->text() == "admin" || edit2->text() == "123456")
{
msgbox.setIcon(QMessageBox::Critical);
msgbox.setWindowTitle("Tomato");
msgbox.setText("用户名和密码不匹配,是否重新登录!!!");
msgbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
ret = msgbox.exec();
if(ret == QMessageBox::Ok)
{
edit2->clear();
edit2->setPlaceholderText("密码");
}
else
{
this->close();
}
}
}
void login::btn2_slot()
{
QMessageBox::StandardButton ret = QMessageBox::question(
this,"Tomato","quit?",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
if(ret == QMessageBox::Yes)
this->close();
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void myslot();
private:
Ui::Form *ui;
};
#endif // FORM_H
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
void Form::myslot()
{
this->show();
}
main.cpp
#include "login.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
login w;
w.show();
Form f;
w.connect(&w,&login::login_success, &f, &Form::myslot);
return a.exec();
}
登录界面
点击join
点击ok
账密错误
点击ok
点击exit