second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>
#include <QMovie>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = nullptr);
~second();
public slots:
void jumpSlot();//对应第一个窗口信号的槽函数声明
private:
Ui::second *ui;
};
#endif // SECOND_H
widget
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void jump(); //自定义一个跳转信号
public slots:
void my_slot();
void my_on();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
mian.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
//实例化第二个窗口的对象
second s;
//将一个窗口的jump信号和第二窗口的jumpSlot槽函数连接
QObject::connect(&w, &Widget::jump, &s, &second::jumpSlot);
return a.exec();
}
second.cpp
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
this->setWindowTitle("300英雄");//设置窗口标题
this->setWindowIcon(QIcon(":/pic/1.png"));//设置窗口图片
QMovie *mv = new QMovie(":/pic/93.gif");//设置图片
ui->label->setMovie(mv);
mv->start();
ui->label->setScaledContents(true);//让图自动使用标签
}
second::~second()
{
delete ui;
}
void second::jumpSlot()
{
this->show();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("300英雄");//设置窗口标题
this->setWindowIcon(QIcon(":/pic/1.png"));//设置窗口图片
ui->label1->setPixmap(QPixmap(":/pic/65.jpg"));//设置图片
ui->label1->setScaledContents(true);//让图自动使用标签
ui->label2->setPixmap(QPixmap(":/pic/5.jpg"));//设置图片
ui->label2->setScaledContents(true);//让图自动使用标签
ui->label3->setPixmap(QPixmap(":/pic/6.jpg"));//设置图片
ui->label3->setScaledContents(true);//让图自动使用标签
ui->label4->setPixmap(QPixmap(":/pic/a.gif"));//设置图片
ui->label4->setScaledContents(true);//让图自动使用标签
ui->username->setPlaceholderText("账号");
ui->password->setPlaceholderText("密码");
ui->password->setEchoMode(QLineEdit::Password);
this->connect(ui->Btn2,SIGNAL(clicked()),this,SLOT(my_slot()));
connect(ui->Btn1,&QPushButton::clicked,this,&Widget::my_on);
}
Widget::~Widget()
{
delete ui;
}
void Widget::my_slot()
{
int ret = QMessageBox::question(this,
"300英雄",//对话框标题
"您是否确定要退出登录?",//对话框文本
QMessageBox::Yes | QMessageBox::No);//提供按钮
if(ret == QMessageBox::Yes)//对用户选中的按钮进行判断
{
this->close();//关闭界面
}
}
void Widget::my_on()
{
if(ui->username->text() == "admin")//账号正确
{
if(ui->password->text() == "123456")//密码正确
{
int ret = QMessageBox::information(this,//直接调用静态成员函数
"300英雄",//对话框标题
"登录成功",//对话框文本
QMessageBox::Ok);//提供按钮
if(QMessageBox::Ok == ret)
{
this->close();//关闭界面
emit jump();//登录成功跳转
}
}
else
{
QMessageBox msg(
QMessageBox::Warning, //图标
"300英雄", //对话框标题
"账号和密码不匹配,是否重新登录", //对话框文本
QMessageBox::Yes | QMessageBox::No, //提供按钮
this);
int ret = msg.exec(); //用exec()函数执行对话框
if(ret == QMessageBox::Yes)//对用户选中的按钮进行判断
{
ui->password->clear();//清空密码框中的内容
}
else
{
this->close();//关闭界面
}
}
}
else
{
QMessageBox msg(
QMessageBox::Warning, //图标
"300英雄", //对话框标题
"账号和密码不匹配,是否重新登录", //对话框文本
QMessageBox::Yes | QMessageBox::No, //提供按钮
this);
int ret = msg.exec();//用exec()函数执行对话框
if(ret == QMessageBox::Yes)//对用户选中的按钮进行判断
{
ui->username->clear();//清空账号框中的内容
ui->password->clear();//清空密码框中的内容
}
else
{
this->close();//关闭界面
}
}
}