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

相关推荐
yaso_zhang5 小时前
当生产了~/qt-arm/bin/qmake,可以单独编译其他-源码的某个模块,如下,编译/qtmultimedia
qt
code bean5 小时前
【Qt/C++】深入理解 Lambda 表达式与 `mutable` 关键字的使用
开发语言·c++·qt
爱看书的小沐18 小时前
【小沐学GIS】基于C++绘制二维瓦片地图2D Map(QT、OpenGL、GIS)
c++·qt·gis·opengl·glfw·glut·二维地图
炬火初现1 天前
Qt 的原理及使用(1)——qt的背景及安装
开发语言·qt
weixin_1101 天前
Qt 无边框窗口,支持贴边分屏
c++·qt
gaoenyang7605251 天前
QT Creator配置Kit
开发语言·qt
3D打印-HUSTAIBO1 天前
QT中connect高级链接——指针、lambda、宏
qt
刘梓谦1 天前
Qt获取CPU使用率及内存占用大小
开发语言·c++·qt
追烽少年x2 天前
Qt中在子线程中刷新UI的方法
qt
猫生鱼2 天前
qml中的TextArea使用QSyntaxHighlighter显示高亮语法
c++·qt