登录窗口
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this -> setFixedSize(540, 410); //固定窗口大小
this -> setWindowTitle("啊啊啊"); //设置窗口标题
this -> setWindowIcon(QIcon(":/icon/1.png")); //设置窗口图标
lab1 = new QLabel(this); //构造一个标签
lab1 -> setPixmap(QPixmap(":/icon/logo.png")); //标签中添加图片
lab1 -> setScaledContents(true); //设置图片自适应
lab1 -> move(0, 0); //标签位置
lab1 -> resize(540,150); //标签大小
lab2 = new QLabel(this); //构建账户标签
lab2 -> setPixmap(QPixmap(":/icon/userName.jpg")); //标签中添加图片
lab2 -> setScaledContents(true); //设置图片自适应
lab2 -> move(80, 200); //标签位置
lab2 -> resize(50,50); //标签大小
lab3 = new QLabel(this); //构建密码标签
lab3 -> setPixmap(QPixmap(":/icon/passwd.jpg")); //标签中添加图片
lab3 -> setScaledContents(true); //设置图片自适应
lab3 -> move(80, 280); //标签位置
lab3 -> resize(50,50); //标签大小
lab4 = new QLabel(this);
lab4 -> move(80, 350); //标签位置
lab4 -> resize(100,20); //标签大小
edit1 = new QLineEdit(this);
edit1 -> setPlaceholderText("QQ号码/手机号/邮箱");
edit1 -> move(180, 200);
edit1 -> resize(300,50);
edit2 = new QLineEdit(this);
edit2 -> setPlaceholderText("密码");
edit2 -> setEchoMode(QLineEdit::Password);
edit2 -> move(180, 280);
edit2 -> resize(300,50);
btn1 = new QPushButton(QIcon(":/icon/login.png"),"登录",this);
btn1 -> move(260, 350);
btn1 -> resize(100, 40);
btn2 = new QPushButton(QIcon(":/icon/cancel.png"),"取消",this);
btn2 -> move(380, 350);
btn2 -> resize(100, 40);
/******************************************************************/
connect(btn1, &QPushButton::clicked, this, &Widget::Loging);
connect(btn2, SIGNAL(clicked()), this, SLOT(close()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::Loging()
{
if("admin" == edit1->text() && "123456" == edit2->text()){
qDebug() << "登录成功";
lab4 -> setText("登陆成功");
emit jump();
this -> close();
}
else if("admin" == edit1->text()){
qDebug() << "密码错误登录失败";
lab4 -> setText("密码错误登录失败");
emit jump_err();
edit2 -> clear();
}
else{
qDebug() << "账号错误";
lab4 -> setText("账号错误");
emit jump_err();
edit2 -> clear();
}
}
主函数
#include "widget.h"
#include"form.h" //第二个界面的头文件
#include"error.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Form f;
error e;
QObject::connect(&w, &Widget::jump_err, &e, &error::jump_slot);
QObject::connect(&w, &Widget::jump, &f, &Form::jump_slot); //将第一个界面的信号函数与第二个界面的槽函数连接
return a.exec();
}
登录成功后的窗口
#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::jump_slot()
{
this->show();
}
登录失败后的窗口
#include "error.h"
#include "ui_error.h"
void error::jump_slot()
{
ui -> lab1 ->setText("登录错误");
connect(ui->btn1, &QPushButton::clicked, this, &error::close);
this -> show();
}
error::error(QWidget *parent) :
QWidget(parent),
ui(new Ui::error)
{
ui->setupUi(this);
}
error::~error()
{
delete ui;
}