1.
界面二头文件: jump.h
#ifndef JUMP_H
#define JUMP_H
#include <QWidget>
#include <QMovie>
namespace Ui {
class Jump;
}
class Jump : public QWidget
{
Q_OBJECT
public:
explicit Jump(QWidget *parent = nullptr);
~Jump();
public slots:
void my_jump_slot();
private:
Ui::Jump *ui;
};
#endif // JUMP_H
界面一头文件:mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QMovie>
#include <QTextEdit>
#include <QDebug>
#include <QMessageBox>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
~MyWidget();
private:
Ui::MyWidget *ui;
signals:
void my_jump();
public slots:
void my_close();
void login_slot();
};
#endif // MYWIDGET_H
界面2功能代码:jump.cpp
#include "jump.h"
#include "ui_jump.h"
Jump::Jump(QWidget *parent) :
QWidget(parent),
ui(new Ui::Jump)
{
ui->setupUi(this);
//创建动图对象
QMovie *mv = new QMovie(":/pictrue/pic.gif");
//加入标签
ui->jump_lab->setMovie(mv);
//启动动图
mv->start();
//自动适应大小
ui->jump_lab->setScaledContents(true);
}
Jump::~Jump()
{
delete ui;
}
//跳转信号的槽处理
void Jump::my_jump_slot()
{
this->show();
}
界面1功能代码
#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
//创建动图对象
QMovie *mv = new QMovie(":/pictrue/pic1.gif");
//加入标签
ui->logo_lab->setMovie(mv);
//启动动图
mv->start();
//自动适应大小
ui->logo_lab->setScaledContents(true);
//加入图片
ui->username_lab->setPixmap(QPixmap(":/pictrue/userName.jpg"));
//自动适应大小
ui->username_lab->setScaledContents(true);
ui->passwd_lab->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->passwd_lab->setScaledContents(true);
//设置占位
ui->username_edit->setPlaceholderText("账号/邮箱/手机");
ui->passwd_edti->setPlaceholderText("密码");
//设置密码输入
ui->passwd_edti->setEchoMode(QLineEdit::Password);
//=======================信号和槽=========================
//手动连接,qt4版本,取消按钮自定义槽函数关闭窗口
connect(ui->cancel_btn,SIGNAL(clicked()),this,SLOT(my_close()));
//手动连接,qt5版本
connect(ui->login_btn,&QPushButton::clicked,this,&MyWidget::login_slot);
}
MyWidget::~MyWidget()
{
delete ui;
}
//手动连接自定义槽函数,qt4版本,关闭窗口
void MyWidget::my_close()
{
//静态成员函数版消息对话框
int ret = QMessageBox::question(this,"问题","你是否确定要退出登录?");
//判断返回按钮值
if(ret == QMessageBox::Yes)
{
this->close();
}
}
//手动连接自定义槽函数,qt5版本,登录
void MyWidget::login_slot()
{
if(ui->username_edit->text() == "admin" && ui->passwd_edti->text() == "123456")
{
//属性版消息对话框
//实例化消息对话框对象msg1
QMessageBox msg1(QMessageBox::NoIcon,"登录状态","登录成功!!!",QMessageBox::Ok,this);
//调用exec显示对话框
int ret = msg1.exec();
//判断按钮
if(ret == QMessageBox::Ok)
{
//触发信号实现跳转界面
emit this->my_jump();
//关闭该界面
this->close();
}
}else {
//属性版消息对话框
//实例化消息对话框对象msg2
QMessageBox msg2(QMessageBox::Critical,"登录状态",
"账号和密码不匹配,是否重新登录?",QMessageBox::Yes|QMessageBox::No,this);
//调用exec
int ret = msg2.exec();
//判断按钮
if(ret == QMessageBox::Yes)
{
ui->passwd_edti->clear(); //登录失败清空密码栏
}else {
this->close();
}
}
}
主函数代码:main.cpp
#include "mywidget.h"
#include "jump.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
//实例化界面对象s
Jump s;
//连接函数,实现两个界面跳转
QObject::connect(&w,&MyWidget::my_jump,&s,&Jump::my_jump_slot);
return a.exec();
}
效果图
登陆成功
登录失败
取消
2.思维导图