qt day2

完善登录框

点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容"账号密码不匹配,是否重新登录",给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

如果账号和密码匹配,则弹出信息对话框,给出提示信息为"登录成功",给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

要求:消息对话框,对象版和静态成员函数版至少各实现一个

登录失败功能、取消键功能

登录成功弹窗、登录后界面跳转

second.h

cpp 复制代码
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
    Q_OBJECT
public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();

private:
    Ui::Second *ui;
public slots:
    void jump_slot();
};
#endif // SECOND_H

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include "second.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void fun();
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QPushButton *btn1;
    QPushButton *btn2;
private slots:
    void login_clicked();
    void cancel_clicked();
private:
    Ui::Widget *ui;
    Second *s1;
signals:
    void jump(); //自定义跳转函数
};
#endif // WIDGET_H

main.cpp

cpp 复制代码
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

second.cpp

cpp 复制代码
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
}
Second::~Second()
{
    delete ui;
}
void Second::jump_slot()
{
    this->show();//展示
}

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    s1=new Second;
    connect(this,&Widget::jump,s1,&Second::jump_slot);
    resize(QSize(960,1200));             //使用匿名对象作为函数参数设置当前组件的尺寸
    setFixedSize(960,1200);                 //设置固定尺寸
    setWindowTitle("miHoYo");               //设置窗口标题
    setWindowIcon(QIcon(":/icon/qq.png"));     //设置窗口图标
    setStyleSheet("background-color:white;");              //设置样式表
    setWindowOpacity(2);                              //设置窗口透明度
    lab1 = new QLabel(this);
    lab1->resize(960,532);           //重新设置尺寸
    lab1->setAlignment(Qt::AlignCenter);
   // lab1->setStyleSheet("background-color:yellow;");
    lab1->setPixmap(QPixmap(":/icon/logo.png"));       //设置图片
    lab1->setScaledContents(true);

    lab2 = new QLabel(this);
    lab2->resize(65,71);           //重新设置尺寸
    lab2->move(lab1->x()+200,lab1->y()+700);
    //lab2->setStyleSheet("background-color:yellow;");
    lab2->setPixmap(QPixmap(":/icon/user.jpg"));       //设置图片
    lab2->setScaledContents(true);

    lab3 = new QLabel(this);
    lab3->resize(65,71);           //重新设置尺寸
    lab3->move(lab2->x(),lab2->y()+150);
    lab3->setAlignment(Qt::AlignCenter);
    //lab3->setStyleSheet("background-color:yellow;");
    lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));       //设置图片
    lab3->setScaledContents(true);

    edit1 = new QLineEdit(this);
    edit1->resize(350,50);
    edit1->move(lab2->x()+120, lab2->y());
    edit1->setEchoMode(QLineEdit::Normal);        //设置回显模式
    edit1->setMaxLength(11);
    edit1->setPlaceholderText("账号/手机/邮箱");         //设置占位文本

    edit2 = new QLineEdit(this);
    edit2->resize(350,50);
    edit2->move(lab3->x()+120, lab3->y());
    edit2->setEchoMode(QLineEdit::Password);        //设置回显模式
    edit2->setMaxLength(10);
    edit2->setPlaceholderText("密码");         //设置占位文本

    btn1 = new QPushButton;//无参构造
    connect(btn1,&QPushButton::clicked,this,&Widget::login_clicked);
    btn1->setParent(this);                     //将当前界面作为父组件
    btn1->setText("登录");                      //设置按钮上的文本内容
    btn1->resize(100,30);                         //重新设置组件尺寸
    btn1->setIcon(QIcon(":/icon/login.png"));    //设置图标
    btn1->move(edit2->x()-20,edit2->y()+150);                          //移动组件位置

    btn2 = new QPushButton;            //无参构造
    connect(btn2,&QPushButton::clicked,this,&Widget::cancel_clicked);
    btn2->setParent(this);                     //将当前界面作为父组件
    btn2->setText("取消");                      //设置按钮上的文本内容
    btn2->resize(100,30);                         //重新设置组件尺寸
    btn2->setIcon(QIcon(":/icon/cancel.png"));    //设置图标
    btn2->move(btn1->x()+250,btn1->y());

}

Widget::~Widget()
{
    delete ui;
}

//登录按钮对应的槽函数
void Widget::login_clicked()
{
    //登录失败
    if(edit1->text()!="admin"&&edit2->text()!="123456")
    {
        //调用构造函数实例化对象
        QMessageBox box(QMessageBox::Critical,//图标
                        "错误",//对话框标题
                        "账号密码不匹配,是否重新登录",//对话框文本
                        QMessageBox::Yes|QMessageBox::No,//提供的按钮
                        this);//父组件
        box.setDefaultButton(QMessageBox::Yes);
        box.setButtonText(QMessageBox::Yes,"OK");
        box.setButtonText(QMessageBox::No,"cancel");
        //调用exec函数运行对话框
        int ret = box.exec();
        //对结果进行判断
        if(ret==QMessageBox::Yes)
        {
            //清空内容
            edit1->clear();
        }
        else
        {
            //关闭界面
            this->close();
        }
    }
    //登录成功
    else
    {
        //调用构造函数实例化对象
        QMessageBox box(QMessageBox::NoIcon,//图标
                        " ",//对话框标题
                        "登录成功",//对话框文本
                        QMessageBox::Ok,//提供的按钮
                        this);//父组件
        box.exec();//运行对话框
        emit jump();//跳转界面
        this->close();//关闭当前界面
    }

}

//退出按钮
void Widget::cancel_clicked()
{
    int ret=QMessageBox::warning(this,
                         "警告",
                         "是否确定要退出登录",
                         QMessageBox::Yes | QMessageBox::No,
                         QMessageBox::No);

    //对结果进行判断如果点yes则关闭
    if(ret==QMessageBox::Yes)
    {
        this->close();//关闭界面
    }
}
相关推荐
cefler几秒前
【QT】QSS基础
开发语言·qt
忒可君几秒前
Qt/C++开发经验
数据库·c++·qt·学习·c#
Q1860000000010 分钟前
Windows 系统中python的安装步骤
开发语言·windows·python
鸽芷咕10 分钟前
【Python报错已解决】AttributeError: ‘Tensor‘ object has no attribute ‘kernel_size‘
开发语言·python·机器学习·bug
小周的C语言学习笔记28 分钟前
鹏哥C语言49---第5次作业:选择语句 if 和 switch
c语言·开发语言·c++·算法
lovPopcorn34 分钟前
MQ高级(二):死信交换机--延迟消息及DelayExchange插件--超时订单案例实现
java·开发语言·rabbitmq
䛩x1 小时前
贪吃蛇小游戏(Qt)
c语言·开发语言·qt·程序人生·游戏程序·命令模式
世界尽头与你1 小时前
Go语言流程控制
开发语言·后端·golang
白茶等风121381 小时前
C#_继承详解
开发语言·c#
You can do more1 小时前
Qt-qmake语言
开发语言·qt