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

相关推荐
娇娇yyyyyy11 小时前
QT编程(13): Qt 事件机制eventfilter
开发语言·qt
带娃的IT创业者13 小时前
工具状态失踪之谜:EventBus事件漏接与asyncio.Lock并发陷阱双线诊断
qt·websocket·并发控制·eventbus·事件驱动架构·pwa·asyncio.lock
不想看见40414 小时前
C++/Qt 代码规范指南
开发语言·qt
li星野15 小时前
QT模拟题:QT项目实践与架构设计(120分钟)
开发语言·qt
笑鸿的学习笔记19 小时前
qt-C++语法笔记之Qt中的delete ui、ui的本质与Q_OBJECT
c++·笔记·qt
不想看见40421 小时前
Qt 框架中的信号与槽机制【详解】
服务器·数据库·qt
行者..................1 天前
第2课:恢复出厂、掌握 Linux 基础命令并完成首次 GCC 编译
linux·qt·driver
Lhan.zzZ1 天前
深入浅出 Qt 信号槽连接方式:从 AutoConnection 到 BlockingQueuedConnectionQt
开发语言·c++·qt
Ronin3051 天前
【Qt窗口】Qt窗口
开发语言·qt·qt窗口
行者..................1 天前
第1课:搭建 Linux 驱动与 Qt 开发基础环境
linux·运维·qt·mpsoc