QT--day5

注册

mainwindow.h

复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include <QMessageBox>
#include<QString>
#include<QSqlDatabase>          //数据库管理类
#include<QSqlQuery>              //执行sql语句的类
#include<QSqlRecord>              //数据库记录的类
#include "form.h"
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QLineEdit *zh;
    QLineEdit *mm;
    Form *f1;
    QSqlDatabase db;
signals:
    void my_sig();
    void jump();
public slots:
    void btn1_slot();
    void btn2_slot();
    void btn3_slot();
};
#endif // MAINWINDOW_H

mainwindow.cpp

复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置固定大小
    this->setFixedSize(400,300);
    //设置窗口标题
    this->setWindowTitle("HUAQI");
    //设置窗口图标
    this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
    //构建两个按钮
    QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
    btn1->resize(100,40);
    btn1->move(150,250);

    QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+125,btn1->y());
    connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));

    QPushButton *btn3=new QPushButton("注册",this);
    btn3->resize(btn1->size());
    btn3->move(btn1->x()-125,btn1->y());
    connect(btn3,SIGNAL(clicked()),this,SLOT(btn3_slot()));
    //构建两个行编辑器
    QLineEdit *edit1=new QLineEdit(this);
    edit1->resize(200,30);
    edit1->setEchoMode(QLineEdit::Password);
    edit1->setText("123456");
    edit1->move(125,btn1->y()-50);
    QLineEdit *edit2=new QLineEdit(this);
    edit2->resize(200,30);
    edit2->setText("admin");
    edit2->move(125,edit1->y()-50);
    //构建三个标签
    QLabel *lab1=new QLabel(this);
    lab1->resize(30,30);
    lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
    lab1->setScaledContents(true);
    lab1->move(edit1->x()-40,edit1->y());

    QLabel *lab2=new QLabel(this);
    lab2->resize(lab1->size());
    lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
    lab2->setScaledContents(true);
    lab2->move(edit2->x()-40,edit2->y());

    QLabel *lab3=new QLabel(this);
    lab3->resize(400,120);
    lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
    lab3->setScaledContents(true);

    connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
    zh=edit2;
    mm=edit1;
    //设置数据库
    if(!db.contains("mydb.db"))
    {
        db=QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("mydb.db");
    }
    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }
    //创建表
    QString sql="create table if not exists stu_info("
            "zhanghao varchar(20) primary key,"
            "mima varchar(20))";
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","创建表失败");
        return;
    }
}

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

void MainWindow::btn1_slot()
{
    QString i=zh->text();
    QString j=mm->text();
    qDebug()<<i;
    qDebug()<<j;
    QString sql=QString("select * from stu_info");
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","查询失败");
        return;
    }
    while(querry.next())
    {
        qDebug()<<querry.record().value(0).toString();
        qDebug()<<querry.record().value(1).toString();
        if(querry.record().value(0).toString()==i&&querry.record().value(1).toString()==j)
        {
            QMessageBox box(QMessageBox::NoIcon,
                            "成功",
                            "登陆成功",
                            QMessageBox::Ok,
                            this);
            int ret=box.exec();
            if(ret==QMessageBox::Ok)
            {
                emit jump();
            }
            return;
        }

    }


    QMessageBox box(QMessageBox::Warning,
                    "错误",
                    "账号或密码出错",
                    QMessageBox::Yes|QMessageBox::No,
                    this);

    box.setButtonText(QMessageBox::Yes,"继续");
    box.setButtonText(QMessageBox::No,"取消");

    int ret=box.exec();
    if(ret==QMessageBox::Yes)
    {

    }else
    {
        this->close();
    }
    return;


}

void MainWindow::btn2_slot()
{
    QMessageBox box(QMessageBox::Question,
                    "退出",
                    "确定要退出吗?",
                    QMessageBox::Yes|QMessageBox::No,
                    this);
    int ret=box.exec();
    if(ret==QMessageBox::Yes)
    {
        this->close();
    }

}

void MainWindow::btn3_slot()
{
    QString zhanghao=zh->text();
    QString mima=mm->text();
    if(zhanghao.isEmpty()||mima.isEmpty())
    {
        QMessageBox::information(this,"提示","信息不完整");
        return;
    }
    QString sql=QString("insert into stu_info(zhanghao,mima)"
                        "values('%1','%2')").arg(zhanghao).arg(mima);
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","添加失败");
        return;
    }else
    {
        QMessageBox::information(this,"成功","添加成功");
    }
}

效果图

添加

登录

相关推荐
VBA63376 分钟前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊15 分钟前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点35 分钟前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
贩卖纯净水.1 小时前
浏览器兼容-polyfill-本地服务-优化
开发语言·前端·javascript
k要开心1 小时前
C++概念以及基础框架语法
开发语言·c++
开发者工具分享2 小时前
如何应对敏捷转型中的团队阻力
开发语言
gregmankiw2 小时前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
roman_日积跬步-终至千里2 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
秦少游在淮海2 小时前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用
AAA废品回收站陈师傅2 小时前
68常用控件_QGroupBox的使用
qt