1> 完成登录框的按钮操作,并在登录成功后进行界面跳转
2> 将模拟面试答案写在思维导图上
代码
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QPushButton>
#include <QObject>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void Jump_Slot();
private:
Ui::Form *ui;
};
#endif // FORM_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QMovie>
#include <QComboBox>
class Widget : public QWidget
{
Q_OBJECT
signals:
void My_Signal(); //自定义信号函数
void Jump_Signal();
public slots:
void Btn_Login_Slot(); //自定义的槽函数
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QMovie *movie;
QLabel *lab_logo;
QLabel *lab_usr;
QLabel *lab_pwd;
QLineEdit *edit_usr;
QLineEdit *edit_pwd;
QPushButton *btn_login;
QPushButton *btn_cancel;
QComboBox *combox;
};
#endif // WIDGET_H
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
connect(ui->closebtn, &QPushButton::clicked, this, &Form::close);
}
Form::~Form()
{
delete ui;
}
void Form::Jump_Slot()
{
this->show();
}
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(540, 420);
this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
qDebug() << this->windowTitle();
this->setWindowTitle("yys");
qDebug() << this->windowTitle();
movie = new QMovie;
movie->setFileName(":/icon/2.gif");
lab_logo = new QLabel(this);
lab_logo->resize(540, 140);
//lab_logo->setPixmap(QPixmap("B:/Users/12898/OneDrive/桌面/icon/logo.png"));
lab_logo->setMovie(movie);
lab_logo->setScaledContents(true);
movie->start();
lab_usr = new QLabel(this);
lab_usr->resize(40, 40);
lab_usr->setPixmap(QPixmap(":/icon/userName.jpg"));
lab_usr->setScaledContents(true);
lab_usr->move(120, 180);
lab_pwd = new QLabel(this);
lab_pwd->resize(40, 40);
lab_pwd->setPixmap(QPixmap(":/icon/passwd.jpg"));
lab_pwd->setScaledContents(true);
lab_pwd->move(lab_usr->x(), lab_usr->y() + 75);
edit_usr = new QLineEdit(this);
//edit1->setText("请输入。。。"); //设置编辑器中的文本内容
edit_usr->setPlaceholderText("QQ/手机/邮箱"); //设置编辑器的占位文本
edit_usr->resize(200, 40); //设置尺寸
edit_usr->move(lab_usr->x() + 80, lab_usr->y()); //移动位置
edit_pwd = new QLineEdit(this);
//edit1->setText("请输入。。。"); //设置编辑器中的文本内容
edit_pwd->setPlaceholderText("密码"); //设置编辑器的占位文本
edit_pwd->resize(200, 40); //设置尺寸
edit_pwd->move(lab_pwd->x() + 80, lab_pwd->y()); //移动位置
edit_pwd->setEchoMode(QLineEdit::Password); //设置回显模式,密文
btn_login = new QPushButton(this); //将当前界面设置成父组件
btn_login->resize(80, 40);
btn_login->setIcon(QIcon(":/icon/login.png"));
btn_login->setText("登录");
btn_login->move(edit_pwd->x() + 80, edit_pwd->y() + 80);
connect(btn_login, &QPushButton::clicked, this, &Widget::Btn_Login_Slot);
btn_cancel = new QPushButton(this); //将当前界面设置成父组件
btn_cancel->resize(80, 40);
btn_cancel->setIcon(QIcon(":/icon/cancel.png"));
btn_cancel->setText("取消");
btn_cancel->move(btn_login->x() + 120, btn_login->y());
connect(btn_cancel, SIGNAL(pressed()), this, SLOT(close()));
combox = new QComboBox(this);
combox->addItem("1");
combox->resize(80, 40);
combox->move(btn_login->x() - 120, btn_login->y());
}
void Widget::Btn_Login_Slot()
{
if(edit_usr->text() == "admin" && edit_pwd->text() == "123456")
{
qDebug() << "登录成功";
emit Jump_Signal();
this->close();
}
else
{
qDebug() << "登录失败";
edit_pwd->clear();
}
}
Widget::~Widget()
{
}
main.cpp
#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_Signal, &f, &Form::Jump_Slot);
return a.exec();
}
结果
思维导图