day48

widget.cpp

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

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H
#include "second.h"
#include <QWidget>
#include <QMessageBox>
#include <QIcon>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
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

相关推荐
QT 小鲜肉17 小时前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
MeowKnight95817 小时前
【Qt】Qt实践记录3——UDP通信
笔记·qt
扶尔魔ocy1 天前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
开心-开心急了1 天前
关于Flutter与Qt for python 的一些技术、开源、商用等问题
开发语言·python·qt·flutter
友友马1 天前
『 QT 』按钮类控件属性解析
开发语言·数据库·qt
QT 小鲜肉1 天前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github
老歌老听老掉牙1 天前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
共享家95272 天前
Qt窗口教程(上)
开发语言·qt
俊俊谢2 天前
OpenCV环境配置(QT 6.6.1 MSVC2019 64bit + OpenCV – 4.12.0)
qt·opencv·msvc
小小鱼儿飞2 天前
QT Quick QML项目音乐播放器16----无边框窗口拖动、小窗播放、隐藏系统托盘
开发语言·qt