day48

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    s1=new second;
    connect(this,&Widget::jump,s1,&second::jump_slot);
    ui->lab1->setPixmap(QPixmap(":/tu/nsh.png"));
    ui->lab1->setScaledContents(true);
    ui->lab2->setPixmap(QPixmap(":/tu/zh.jpg"));
    ui->lab2->setScaledContents(true);
    ui->lab3->setPixmap(QPixmap(":/tu/mm.png"));
    ui->lab3->setScaledContents(true);
    if(!db.contains("mydatabase.db"))
    {
        db=QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("mydatabase.db");
    }
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }
    QString sql = "create table if not exists stu_info("
                  "name integer primary key,"
                  "password integer)";
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","创建表失败");
        return;
    }
}

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


void Widget::on_buf1_clicked()
{
    QString sql = "select * from stu_info where name";
    QString sq = "select * from stu_info where password";
    QSqlQuery querry;
    QSqlQuery quess;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"提示","登录失败");
        return;
    }
    if(!quess.exec(sq))
    {
        QMessageBox::information(this,"提示","登录失败");
        return;
    }
   //遍历账号
    int i=0;
    while(querry.next()&&quess.next())
    {
        //判断
        if(ui->lin1->text()==querry.record().value(0).toString()&&ui->lin2->text()==quess.record().value(0).toString())
        {
            QMessageBox box(QMessageBox::Information,
                            "信息对话框",
                            "登录成功",
                            QMessageBox::Ok,
                            this);
            int ret = box.exec();
            if(ret == QMessageBox::Ok)
            {
                emit jump();
                this->close();
            }
            i=1;
            break;
        }
    }
    if(i==0)
    {
            QMessageBox box(QMessageBox::Warning,
                            "错误对话框",
                            "账号密码不匹配是否重新登录",
                            QMessageBox::Ok|QMessageBox::Cancel,
                            this);
            int ret = box.exec();
            if(ret == QMessageBox::Ok)
            {
                //清空密码
                ui->lin2->clear();
            }
            else if(ret == QMessageBox::Cancel)
            {
                this->close();
            }
    }
}

void Widget::on_buf2_clicked()
{
    int ret=QMessageBox::question(this,
                                  "问题",
                                  "是否确认退出",
                                  QMessageBox::Yes|QMessageBox::No,
                                  QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else if(ret==QMessageBox::No)
    {

    }
}

void Widget::on_buf3_clicked()
{
    int name=ui->lin1->text().toUInt();
    int password=ui->lin2->text().toUInt();
    if(name==0||password==0)
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
        return;
    }
    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,"成功","添加成功");
    }
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include "second.h"
#include 
#include 
#include 
#include 
#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
signals:
    void jump();

private slots:
    void on_buf1_clicked();
    void on_buf2_clicked();
    void on_buf3_clicked();

private:
    Ui::Widget *ui;
    second *s1;
    QSqlDatabase db;
};
#endif // WIDGET_H

 

day48_第1张图片

day48_第2张图片day48_第3张图片

你可能感兴趣的:(qt)