qt完善登录框

完善登录框

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

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

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

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

widget.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    c = new com;
    connect(this, &MainWindow::jump, c, &com::jump_slot);

    //设置窗口标题及图像
    this->setFixedSize(480, 300);
    this->setWindowTitle("华清远见");
    this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));

    //设置背景
    QLabel* lab1 = new QLabel(this);
    lab1->resize(480,140);
    lab1->setPixmap(QPixmap(":/icon/logo.png"));
    lab1->setScaledContents(true);

    //设置标签
    QLabel* lab2 = new QLabel(this);
    QLabel* lab3 = new QLabel(this);
    lab2->resize(32, 36);
    lab2->move(50, 160);
    lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
    lab2->setScaledContents(true);
    lab3->resize(32, 36);
    lab3->move(50, 210);
    lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
    lab3->setScaledContents(true);

    //设置行文本
    lin1 = new QLineEdit(this);
    lin1->resize(270, 32);
    lin1->move(lab2->x()+40,lab2->y()+2);
    lin1->setPlaceholderText("账号/邮箱/手机号");

    lin2 = new QLineEdit(this);
    lin2->resize(270, 32);
    lin2->move(lab3->x()+40,lab3->y()+2);
    lin2->setEchoMode(QLineEdit::Password);

    //设置按钮
    but1 = new QPushButton(this);
    but1->resize(80, 35);
    but1->move(120, 255);
    but1->setIcon(QIcon(":/icon/login.png"));
    but1->setText("登录");

    but2 = new QPushButton(this);
    but2->resize(80, 35);
    but2->move(280, 255);
    but2->setIcon(QIcon(":/icon/cancel.png"));
    but2->setText("取消");

    //设置选项
    QCheckBox *rad1 = new QCheckBox(this);
    rad1->move(lin1->x()+280,lin1->y());
    rad1->setText("自动登录");
    rad1->resize(100, 32);

    QCheckBox *rad2 = new QCheckBox(this);
    rad2->move(lin2->x()+280,lin2->y());
    rad2->setText("记住密码");
    rad2->resize(100, 32);



    connect(but1,&QPushButton::clicked,[=](){

        if(lin1->text()=="admin" && lin2->text()=="123456")
        {
            QMessageBox succbox(QMessageBox::NoIcon,"登录","登录成功",QMessageBox::Ok,but1);
            int err = succbox.exec();
            if(err == QMessageBox::Ok)
            {
                emit jump();
                this->close();
               // emit jump();
            }
        }
        else
        {
            int err = QMessageBox::critical(
                        this,tr("错误"),
                        tr("账号密码不匹配,是否重新登录"),
                        QMessageBox::Yes | QMessageBox::Cancel,
                        QMessageBox::Cancel
                        );

            if(err == QMessageBox::Cancel)
            {
                int ques = QMessageBox::question(
                            this,
                            tr("问题"),
                            tr("是否退出"),
                            QMessageBox::Yes | QMessageBox::No,
                            QMessageBox::No
                            );
                if(ques == QMessageBox::Yes)
                {
                    this->close();
                }
            }
            this->lin1->clear();
            this->lin2->clear();
        }
    });
}

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

widget.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "com.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QPushButton* but1;
    QPushButton* but2;
    QLineEdit *lin1;
    QLineEdit *lin2;

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    com* c;

signals:
    void jump();
};
#endif // MAINWINDOW_H

com.cpp

#include "com.h"
#include "ui_com.h"

void com::jump_slot()
{
    this->show();
}

com::com(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::com)
{
    ui->setupUi(this);

}

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

 com.h

#ifndef COM_H
#define COM_H

#include 

namespace Ui {
class com;
}

class com : public QWidget
{
    Q_OBJECT

public slots:
    void jump_slot();


public:
    explicit com(QWidget *parent = nullptr);
    ~com();

private:
    Ui::com *ui;
};

#endif // COM_H

你可能感兴趣的:(qt,命令模式,算法)