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,"成功","添加成功");
    }
}

效果图

添加

登录

相关推荐
疯狂的挖掘机10 小时前
记一次基于QT的图片操作处理优化思路(包括在图上放大缩小,截图,画线,取值等)
开发语言·数据库·qt
cnxy18810 小时前
围棋对弈Python程序开发完整指南:步骤4 - 提子逻辑和劫争规则实现
开发语言·python·机器学习
意趣新10 小时前
C 语言源文件从编写完成到最终生成可执行文件的完整、详细过程
c语言·开发语言
李艺为11 小时前
根据apk包名动态修改Android品牌与型号
android·开发语言
奇树谦11 小时前
Qt | 利用map创建多个线程和定时器
网络·数据库·qt
黄河滴滴11 小时前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
老华带你飞12 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
superman超哥12 小时前
Rust Workspace 多项目管理:单体仓库的优雅组织
开发语言·rust·多项目管理·rust workspace·单体仓库
kylezhao201912 小时前
C#通过HSLCommunication库操作PLC用法
开发语言·c#
JIngJaneIL13 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端