1.头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QMovie>
#include <QLineEdit>
#include <QApplication>
#include <QMessageBox>
#include <iostream>
#include <QDebug>
using namespace std;
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton *ptr1;
QPushButton *ptr2;
QLabel *ptr3;
QLabel *ptr4;
QLabel *ptr5;
QLineEdit *ptr6;
QLineEdit *ptr7;
QMovie *movie;
signals:
void jump();
void my_signal();
void my_signal_1();
public:
Widget(QWidget *parent = nullptr);
~Widget();
void setupUI();
void onLoginClicked();
void onCancelClicked();
void clicked();
void clicked_1();
};
#endif // WIDGET_H
2.源文件
#include "widget.h"
Widget::Widget(QWidget *parent)//构造函数
: QWidget(parent)//调用父类的构造函数进行初始化
{
this->setWindowTitle("Metal Gear Solid V");//窗口名字
this->setWindowIcon(QIcon("://new/1.png"));//窗口图标
this->setFixedSize(600,800);//窗口大小
this->setStyleSheet("background-color:white");//窗口背景色
setupUI();//调用ui函数
}
Widget::~Widget()//析构函数
{
}
void Widget::setupUI() //ui函数
{
ptr1 = new QPushButton("登录",this);//登录按钮
ptr1->resize(100,40);//大小
ptr1->move(120,700);//位置
ptr1->setStyleSheet("background-color: white; color: black;");//颜色
ptr2 = new QPushButton("取消",this);//取消按钮
ptr2->resize(100,40);//大小
ptr2->move(400,700);//位置
ptr2->setStyleSheet("background-color: white; color: black;");//颜色
ptr3 = new QLabel(this);//动图标签
ptr3->resize(600,300);//大小
ptr3->move(0,0);//位置
movie = new QMovie(":/new/2.gif");//动图资源
ptr3->setMovie(movie);
movie->start();
ptr3->setScaledContents(true);
ptr4 = new QLabel("账号:",this);//账号标签
ptr4->move(ptr1->x(),ptr1->y()-200);//位置
ptr4->resize(100,40);//大小
ptr4->setStyleSheet("color: black;");//颜色
ptr5 = new QLabel("密码:",this);//密码标签
ptr5->move(ptr1->x(),ptr1->y()-150);//位置
ptr5->resize(100,40);//大小
ptr5->setStyleSheet("color: black;");//颜色
ptr6 = new QLineEdit(this);//账号单行行编辑器
ptr6->resize(300,40);//大小
ptr6->move(ptr4->x()+50,ptr4->y());//位置
ptr6->setStyleSheet("color: black;");//颜色
ptr7 = new QLineEdit(this);//密码单行行编辑器
ptr7->resize(300,40);//大小
ptr7->move(ptr5->x()+50,ptr5->y());//位置
ptr7->clear();//清空
ptr7->setPlaceholderText("输入密码");//输入提示
ptr7->setStyleSheet("color: black;");//颜色
ptr7->setEchoMode(QLineEdit::Password);//将回显模式设置为密码模式
connect(ptr1, &QPushButton::clicked, this, &Widget::onLoginClicked); // 连接登录按钮
connect(ptr2, &QPushButton::clicked, this, &Widget::onCancelClicked); // 连接取消按钮
connect(this,&Widget::my_signal,this,&Widget::clicked);
connect(this,&Widget::my_signal_1,this,&Widget::clicked_1);
}
void Widget::onCancelClicked()//退出功能的槽函数
{
QMessageBox box(QMessageBox::Question,//图标,问题图标
"退出",//对话框标题
"确认退出?",//对话框文本内容
QMessageBox::Ok|QMessageBox::No,//对话框提供的按钮
this);//父组件
box.setButtonText(QMessageBox::Ok,"是");
box.setButtonText(QMessageBox::No,"不");
box.setDefaultButton(QMessageBox::No);
//显示对话框,返回用户点击的按钮值
int num = box.exec();
//判断用户点击的按钮值
if(num == QMessageBox::Ok)
{
qApp->quit();
}
}
void Widget::onLoginClicked()//登录功能的槽函数
{
QString usr = "12345";
QString pwd = "12345";
QString username = ptr6->text();
QString password = ptr7->text();
if ((username == usr) && (password == pwd))
{
emit my_signal();
}
else
{
emit my_signal_1();
}
}
void Widget::clicked()
{
QMessageBox::information(this, "登录成功", "欢迎登录!", QMessageBox::Ok);
this->close();
emit jump();//发射自定义跳转信号
}
void Widget::clicked_1()
{
QMessageBox msgBox(QMessageBox::Warning, "警告", "账号或密码错误,请重新输入",
QMessageBox::Ok, this);
msgBox.exec();
}
3.主函数
#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();
}
4.思维导图