• day2_QT


    day2_QT

    登录窗口

    loginwindow.h

    #ifndef LOGINWINDOW_H
    #define LOGINWINDOW_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "homepage.h"
    
    
    class LoginWindow : public QWidget
    {
        Q_OBJECT
    
    
    private slots:
        void get_accountAndpwd();
    
    public:
        LoginWindow(QWidget *parent = nullptr);
        ~LoginWindow();
    
    
    private:
        QPushButton *login;
        QPushButton *exit;
        QLineEdit *editpwd;
        QLabel *labelpwd;
        QLineEdit *editaccount;
        QLabel *labelaccount;
        QLabel *labelpic;
    
        HomePage *homepage;
    
    };
    #endif // LOGINWINDOW_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    loginwindow.c

    #include "loginwindow.h"
    #include 
    
    
    
    
    LoginWindow::LoginWindow(QWidget *parent)
        : QWidget(parent)
    {
        //设置窗口标题和图标
        this->setWindowTitle("ChatWe");
        this->setWindowIcon(QIcon(":/login.png"));
    
        //设置窗口的大小 固定
        this->setFixedSize(800,500);
    
        //设置上1/2处label图标放置背景图
        this->labelpic = new QLabel(this);
        //一半大小
        this->labelpic->resize(800,250);
        //放入图片
        this->labelpic->setPixmap(QPixmap(":/bg.png"));
    
        /***********************************************************************/
    
        //账号图标
        this->labelaccount = new QLabel(this);
        this->labelaccount->resize(50,50);
        //移动位置
        this->labelaccount->move(150,280);
        this->labelaccount->setPixmap(QPixmap(":/account.png"));
        this->labelaccount->setScaledContents(true);
    
        //账号编辑框
        this->editaccount = new QLineEdit(this);
        this->editaccount->setPlaceholderText("请输入账号");
        this->editaccount->setAlignment(Qt::AlignCenter);
        this->editaccount->resize(300,50);
        this->editaccount->setMaxLength(20);
        this->editaccount->move(labelaccount->x()+100,labelaccount->y());
    
        //密码图标
        this->labelpwd = new QLabel(this);
        this->labelpwd->resize(50,50);
        //移动位置
        this->labelpwd->move(labelaccount->x(),labelaccount->y()+80);
        this->labelpwd->setPixmap(QPixmap(":/psw.png"));
        this->labelpwd->setScaledContents(true);
    
        //密码编辑框
        this->editpwd = new QLineEdit(this);
        this->editpwd->setPlaceholderText("请输入密码");
        this->editpwd->setEchoMode(QLineEdit::Password);
        this->editpwd->setAlignment(Qt::AlignCenter);
        this->editpwd->resize(300,50);
        this->editpwd->setMaxLength(20);
        this->editpwd->move(editaccount->x(),editaccount->y()+80);
    
        //登录按钮
        this->login = new QPushButton(QIcon(":/accept.png"),"登录",this);
        this->login->move(editpwd->x()+10,editpwd->y()+80);
    
        //退出按钮
        this->exit = new QPushButton(QIcon(":/reject.png"),"退出",this);
        this->exit->move(editpwd->x()+180,editpwd->y()+80);
    
        /*QT5版本 将clicked 信号发送给自定义的槽函数判断账号和密码*/
        this->connect(this->login,&QPushButton::clicked,this, &LoginWindow::get_accountAndpwd);
    
    }
    
    //功能函数,获取账号和密码
    void LoginWindow::get_accountAndpwd()
    {
        QString username = this->editaccount->text();
        QString password = this->editpwd->text();
        if(username == "admin" && password == "123456"){
            //密码正确
            QMessageBox successBox(QMessageBox::Information,"success","登录成功",
                                            QMessageBox::Ok|QMessageBox::Cancel);
            successBox.setWindowIcon(QIcon(":/login.png"));
            int ret = successBox.exec();
            if(ret == QMessageBox::Ok){
                //关闭当前页面
                this->hide();
                //跳转到主页
                this->homepage = new HomePage();
                homepage->show();
            }else {
                //弹出询问界面
                QMessageBox exitBox(QMessageBox::Question,"quit","是否退出?",
                                                QMessageBox::Yes|QMessageBox::No);
                exitBox.setWindowIcon(QIcon(":/login.png"));
                int ret = exitBox.exec();
                if(ret == QMessageBox::Yes){
                    //关闭当前页面
                    this->hide();
                    //跳转到主页
                    this->homepage = new HomePage();
                    homepage->show();
                }else{
                    //退出程序
                    this->close();
                }
            }
    
        }else{
            //账号密码 不匹配 弹出错误对话框
            int ret = QMessageBox::critical(this,tr("ChatWe"),tr("账号密码不匹配.\n""是否重新登录?"),
                                            QMessageBox::Ok|QMessageBox::Cancel,  //两个选项
                                            QMessageBox::Ok);                     //默认选项
            if(ret == QMessageBox::Ok){
                //清空两个框
                this->editaccount->clear();
                this->editpwd->clear();
            }else {
                //退出程序
                this->close();
            }
        }
    }
    
    
    LoginWindow::~LoginWindow()
    {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
  • 相关阅读:
    【C++ 学习】链接、作用域、内存管理
    消息队列系列6-RabbitMQ使用姿势 (荣耀典藏版)
    【爬虫逆向】Python逆向采集猫眼电影票房数据
    Django国际化与本地化指南
    VUE3中defineExpose的使用方法
    Matlab/simulink光伏发电的恒定电压法MPPT仿真(持续更新)
    Element表格之表头合并、单元格合并
    想知道这个电路的工作原理~
    马尔可夫链文本生成预测
    基于JAVA旅游景点推荐系统计算机毕业设计源码+数据库+lw文档+系统+部署
  • 原文地址:https://blog.csdn.net/weixin_45790676/article/details/133001232