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

相关推荐
锦亦之22339 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
柳鲲鹏12 小时前
编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MinGW版)
开发语言·qt·mysql
三玖诶12 小时前
如何在 Qt 的 QListWidget 中逐行添加和显示数据
开发语言·qt
阳光开朗_大男孩儿18 小时前
DBUS属性原理
linux·服务器·前端·数据库·qt
Alphapeople19 小时前
Qt Modbus
开发语言·qt
竹林海中敲代码19 小时前
Qt Creator 集成开发环境 常见问题
qt·qt工具常见问题
竹林海中敲代码1 天前
Qt安卓开发连接手机调试(红米K60为例)
android·qt·智能手机
长沙红胖子Qt1 天前
关于 Qt运行加载内存较大崩溃添加扩大运行内存 的解决方法
开发语言·qt·qt扩大运行内存
gopher95111 天前
qt相关面试题
开发语言·qt·面试
三玖诶1 天前
在 Qt 中使用 QLabel 设置 GIF 动态背景
开发语言·qt·命令模式