QT&C++ day12

注册登录界面

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QMessageBox>//消息对话框类
#include "second.h"
#include <QSqlDatabase>//数据库管理类
#include <QSqlQuery>//执行sql语句类
#include <QSqlRecord>//数据库记录的类
#include "zhuce.h" //注册头文件
QT_BEGIN_NAMESPACE
namespace Ui { class widget; }
QT_END_NAMESPACE

class widget : public QWidget
{
    Q_OBJECT
signals://该权限下定义属于自己的信号
    void my_signal(QString msg);//自定义一个有参无返回值的信号函数
private:
    void on_btn1_clicked();//自定义的槽函数声明
    void on_btn2_clicked();//自定义的槽函数声明
    void on_btn3_clicked();
public:
    widget(QWidget *parent = nullptr);
    ~widget();
signals:
    void jump();//自定义跳转信号函数
    void jump1();
public:
     //void jump_slot2();

private:
    Ui::widget *ui;
    //自定义一个btn1
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QLineEdit *edit2;
    QLineEdit *edit1;
    Second *s1;
    QSqlDatabase db; //定义一个数据库的类对象
    zhuce *s2;
};
#endif // WIDGET_H

zhuce.h

#ifndef ZHUCE_H
#define ZHUCE_H

#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QIcon>
#include <QPushButton>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>//执行sql语句类
#include <QSqlRecord>//数据库记录的类
//#include "widget.h"
namespace Ui {
class zhuce;
}

class zhuce : public QWidget
{
    Q_OBJECT
private:
    void on_btn2_clicked();//自定义的槽函数声明
    void on_btn3_clicked();
public:
     void jump_slot1();
signals:
     //void jump2();

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

private:
    Ui::zhuce *ui;
    QLineEdit *edit2;//密码
    QLineEdit *edit1;//用户名
    QLineEdit *edit3;//确认密码
    QPushButton *btn2;
    QPushButton *btn3;
    QSqlDatabase db; //定义一个数据库的类对象
    //widget *s3;
};

#endif // ZHUCE_H

widget.cpp

#include "widget.h"


widget::widget(QWidget *parent)
    : QWidget(parent)
{
    //登录成功的界面
    s1=new Second;
    connect(this,&widget::jump,s1,&Second::jump_slot);

    //注册的界面
    s2=new zhuce;
    connect(this,&widget::jump1,s2,&zhuce::jump_slot1);

    //添加数据库
    if(!db.contains("mysql.db"))
    {
        //添加数据库
        db=QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库名字
        db.setDatabaseName("mysql.db");
    }

    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }

    //准备sql语句对表进行创建
    QString sql="create table if not exists stu_info("
                "name varchar(10) primary key,"//用户名
                "password varchar(20))";//密码
    //语句执行者
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","数据库创建失败");
        return;
    }

    this->setFixedSize(550,400);//设置固定尺寸
    this->setWindowTitle("Widget");//设置窗口标题
    this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标

    //实例化一个标签
    QLabel *lab1 = new QLabel;
    lab1->setParent(this);
    lab1->resize(550,215);//重新设置尺寸
    //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    lab1->setPixmap(QPixmap("D:\\icon\\logo.png"));
    lab1->setScaledContents(true);//设置内容自适应

    QLabel *lab3 = new QLabel;
    lab3->setParent(this);//指定父组件
    lab3->resize(40,40);//重新设置尺寸
    lab3->move(100,220);
    //lab3->setAlignment(Qt::AlignCenter);
    lab3->setPixmap(QPixmap("D:\\icon\\userName.jpg"));
    lab3->setScaledContents(true);//设置内容自适应

    QLabel *lab2 = new QLabel;
    lab2->setParent(this);
    lab2->resize(40,40);//重新设置尺寸
    lab2->move(100,270);

    lab2->setPixmap(QPixmap("D:\\icon\\passwd.jpg"));
    lab2->setScaledContents(true);//设置内容自适应

    // 实例化一个行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(250,40);
    edit1->move(lab3->x()+50,lab3->y());
    // 实例化一个行编辑器
    edit2 = new QLineEdit(this);
    edit2->resize(250,40);
    edit2->move(lab2->x()+50,lab2->y());
    edit2->setEchoMode(QLineEdit::Password);//设置回显模式

    //实例化一个按钮并给定图标,文本内容,父组件
    btn1 = new QPushButton(QIcon("D:\\icon\\login.png"), "登录", this);
    btn1->resize(100,50);//设置按钮大小
    btn1->move(225,325);//设置按钮移动位置
    connect(this->btn1,&QPushButton::clicked,this,&widget::on_btn1_clicked);

    btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    btn3->resize(btn1->size());//设置按钮大小
    btn3->move(btn1->x()-150,btn1->y());//设置按钮移动位置
    connect(this->btn3,&QPushButton::clicked,this,&widget::on_btn3_clicked);

    btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+150,btn1->y());
    connect(this->btn2,&QPushButton::clicked,this,&widget::on_btn2_clicked);
}
//登录按钮
void widget::on_btn1_clicked()
{
    //获取界面姓名
       QString name = this->edit1->text();
       //准备sql语句
       QString sql = QString("select * from stu_info where name='%1'").arg(name);
       //准备执行者
       QSqlQuery querry;

       if(!querry.exec(sql))
       {
           QMessageBox::information(this, "提示", "登录失败");
           return;
       }
    else
    {
        //1、调用构造函数实例化对象
        QMessageBox box(QMessageBox::Information,//图标
                        "信息对话框",//对话框标题
                        "登录成功",//对话框文本内容
                        QMessageBox::Ok,//提供的按钮
                        this);//父组件
        box.setDefaultButton(QMessageBox::Ok);//将OK设置为默认按钮
        //2、调用exec函数运行对话框
        int ret=box.exec();
        //3、对结果进行判断
        if(ret==QMessageBox::Ok)
        {
            emit jump();
            this->hide();
        }
    }
}
//取消按钮
void widget::on_btn2_clicked()
{
    //1、调用构造函数实例化对象
    QMessageBox box(QMessageBox::Question,//图标
                    "问题对话框",//对话框标题
                    "是否确定取消登录?",//对话框文本内容
                    QMessageBox::Yes|QMessageBox::No,//提供的按钮
                    this);//父组件
    box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    //2、调用exec函数运行对话框
    int ret=box.exec();
    //3、对结果进行判断
    if(ret==QMessageBox::Yes)
    {
        close();
    }
    else if(ret==QMessageBox::No)
    {
    }
}
void widget::on_btn3_clicked()
{
    emit jump1();
    //QWidget::hide();
}
widget::~widget()
{
}

/*void widget::jump_slot2()
{
    this->show();
}*/

zhuce.cpp

#include "zhuce.h"
#include "ui_zhuce.h"


zhuce::zhuce(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::zhuce)
{
    //注册的界面
    //s3=new widget;
    //connect(this,&zhuce::jump2,s3,&widget::jump_slot2);

    this->setFixedSize(550,500);//设置固定尺寸
    this->setWindowTitle("注册");//设置窗口标题
    this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标

    //实例化一个标签
    QLabel *lab = new QLabel;
    lab->setParent(this);
    lab->resize(550,215);//重新设置尺寸
    //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    lab->setPixmap(QPixmap("D:\\icon\\logo.png"));
    lab->setScaledContents(true);//设置内容自适应

    ui->setupUi(this);
    QLabel *lab1 = new QLabel;
    lab1->setParent(this);//指定父组件
    lab1->resize(150,40);//重新设置尺寸
    lab1->move(100,220);
    //lab3->setAlignment(Qt::AlignCenter);
    lab1->setText("用户名:");
    lab1->setScaledContents(true);//设置内容自适应

    QLabel *lab2 = new QLabel;
    lab2->setParent(this);
    lab2->resize(150,40);//重新设置尺寸
    lab2->move(100,270);

    lab2->setText("密码:");
    lab2->setScaledContents(true);//设置内容自适应

    QLabel *lab3 = new QLabel;
    lab3->setParent(this);
    lab3->resize(150,40);//重新设置尺寸
    lab3->move(100,320);
    lab3->setText("确认密码:");
    lab3->setScaledContents(true);//设置内容自适应

    // 实例化一个行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(250,40);
    edit1->move(lab1->x()+80,lab1->y());
    // 实例化一个行编辑器
    edit2 = new QLineEdit(this);
    edit2->resize(250,40);
    edit2->move(lab2->x()+80,lab2->y());
    edit2->setEchoMode(QLineEdit::Password);//设置回显模式

    edit3 = new QLineEdit(this);
    edit3->resize(250,40);
    edit3->move(lab3->x()+80,lab3->y());
    edit3->setEchoMode(QLineEdit::Password);//设置回显模式

    btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    btn3->resize(100,50);//设置按钮大小
    btn3->move(125,400);//设置按钮移动位置
    connect(this->btn3,&QPushButton::clicked,this,&zhuce::on_btn3_clicked);

    btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    btn2->resize(100,50);
    btn2->move(325,400);
    connect(this->btn2,&QPushButton::clicked,this,&zhuce::on_btn2_clicked);

    //添加数据库
        if(!db.contains("mysql.db"))
        {
            //添加数据库
            db=QSqlDatabase::addDatabase("QSQLITE");
            //设置数据库名字
            db.setDatabaseName("mysql.db");
        }

    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }
}

zhuce::~zhuce()
{
    delete ui;
}
//取消
void zhuce::on_btn2_clicked()
{
    //1、调用构造函数实例化对象
    QMessageBox box(QMessageBox::Question,//图标
                    "问题对话框",//对话框标题
                    "是否确定取消注册?",//对话框文本内容
                    QMessageBox::Yes|QMessageBox::No,//提供的按钮
                    this);//父组件
    box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    //2、调用exec函数运行对话框
    int ret=box.exec();
    //3、对结果进行判断
    if(ret==QMessageBox::Yes)
    {
        close();
    }
    else if(ret==QMessageBox::No)
    {
    }
}
void zhuce::on_btn3_clicked()
{
    //记录要录入的数据
    QString name=this->edit1->text();
    QString password=this->edit3->text();
    if(this->edit2->text()!=this->edit3->text()&&this->edit2->text()!=0&&this->edit3->text()!=0)
    {
        QMessageBox::information(this,"失败","请输入相同的密码");
        return;
    }
    //准备sql语句
    QString sql=QString("insert into stu_info(name,password)"
                        "values('%1','%2')").arg(name).arg(password);
    //准备执行语句者
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","注册失败");
        return;
    }
    else
    {
         QMessageBox::information(this,"成功","注册成功");
         //emit jump2();
         this->edit1->clear();
         this->edit2->clear();
         this->edit3->clear();
         this->close();
         //parentWidget()->show();
    }
}
void zhuce::jump_slot1()
{
    this->show();
}

思维导图:

猜你喜欢

转载自blog.csdn.net/m0_59031281/article/details/133148740