1.完成登录框的按钮操作,并在登录成功后进行界面跳转
跳转界面代码:
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
//窗口
this->setFixedSize(1280,768); //锁定窗口大小
this->setWindowTitle("Devil May Cry 5"); //窗口标题
this->setWindowIcon(QIcon(":/cion/th (1).jpg")); //窗口图标
//LOGL1
lab1 = new QLabel(this); //定义标签,依附在界面上
lab1->resize(this->size());
lab1->setPixmap(QPixmap(":/cion/20230831190319.jpg")); //标签图标
lab1->setScaledContents(true); //图标自适应
}
Form::~Form()
{
delete ui;
}
void Form::jump_slot()
{
this->show();
}
登录页面代码:
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//窗口
this->setFixedSize(550,650); //锁定窗口大小
this->setWindowTitle("Devil May Cry 5"); //窗口标题
this->setWindowIcon(QIcon(":/cion/th (1).jpg")); //窗口图标
//LOGL1
lab3 = new QLabel(this); //定义标签,依附在界面上
lab3->resize(550,340);
lab3->setPixmap(QPixmap(":/cion/OIP-C.jpg")); //标签图标
lab3->setScaledContents(true); //图标自适应
//LOGL2
lab4 = new QLabel(this); //定义标签,依附在界面上
lab4->resize(550,310);
lab4->setPixmap(QPixmap(":/cion/OIP-C (1).jpg")); //标签图标
lab4->setScaledContents(true); //图标自适应
lab4->move(0,340);
//登录按钮
btn1 = new QPushButton("登录", this); //定义“登录”按钮,依附在界面上
btn1->setIcon(QIcon(":/cion/login.png")); //按钮图标
btn1->resize(120,50);
btn1->move(160,330);
connect(btn1, &QPushButton::clicked,[&](){
QString name = edit1->text();
QString pass = edit2->text();
if(name == "admin" && pass == "123456")
{
qDebug() << "登陆成功";
emit jump();
this->close();
}
else
{
qDebug() << "登录失败";
edit2->clear();
}
});
//取消按钮
btn2 = new QPushButton("取消", this); //定义“取消”按钮,依附在界面上
btn2->setIcon(QIcon(":/cion/cancel.png")); //按钮图标
btn2->resize(120,50);
btn2->move(btn1->x()+140,btn1->y());
connect(btn2, SIGNAL(pressed()), this, SLOT(close())); //关闭登录界面
//账户图标
lab1 = new QLabel(this); //定义标签,依附在界面上
lab1->resize(50,40);
lab1->move(120,190);
lab1->setPixmap(QPixmap(":/cion/userName.jpg")); //标签图标
lab1->setScaledContents(true); //图标自适应
//密码图标
lab2 = new QLabel(this); //定义标签,依附在界面上
lab2->resize(50,40);
lab2->move(lab1->x(),lab1->y()+75);
lab2->setPixmap(QPixmap(":/cion/passwd.jpg")); //标签图标
lab2->setScaledContents(true); //图标自适应
//账户行
edit1 = new QLineEdit(this); //定义行编辑器,依附在界面上
edit1->setPlaceholderText("if you want it");
edit1->resize(240,40);
edit1->move(lab1->x()+65,lab1->y());
//密码行
edit2 = new QLineEdit(this); //定义行编辑器,依附在界面上
edit2->setPlaceholderText("then you'll have to take it");
edit2->resize(240,40);
edit2->move(lab2->x()+65,lab2->y());
edit2->setEchoMode(QLineEdit::Password); //设置密码回显模式
}
Widget::~Widget()
{
}
主函数:
#include "widget.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Form f;
QObject::connect(&w, &Widget::jump, &f, &Form::jump_slot);
return a.exec();
}
登录页面:
登录跳转:
思维导图: