QT--day5

注册

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include
#include
#include
#include 
#include
#include          //数据库管理类
#include              //执行sql语句的类
#include              //数据库记录的类
#include "form.h"
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QLineEdit *zh;
    QLineEdit *mm;
    Form *f1;
    QSqlDatabase db;
signals:
    void my_sig();
    void jump();
public slots:
    void btn1_slot();
    void btn2_slot();
    void btn3_slot();
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置固定大小
    this->setFixedSize(400,300);
    //设置窗口标题
    this->setWindowTitle("HUAQI");
    //设置窗口图标
    this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
    //构建两个按钮
    QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
    btn1->resize(100,40);
    btn1->move(150,250);

    QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+125,btn1->y());
    connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));

    QPushButton *btn3=new QPushButton("注册",this);
    btn3->resize(btn1->size());
    btn3->move(btn1->x()-125,btn1->y());
    connect(btn3,SIGNAL(clicked()),this,SLOT(btn3_slot()));
    //构建两个行编辑器
    QLineEdit *edit1=new QLineEdit(this);
    edit1->resize(200,30);
    edit1->setEchoMode(QLineEdit::Password);
    edit1->setText("123456");
    edit1->move(125,btn1->y()-50);
    QLineEdit *edit2=new QLineEdit(this);
    edit2->resize(200,30);
    edit2->setText("admin");
    edit2->move(125,edit1->y()-50);
    //构建三个标签
    QLabel *lab1=new QLabel(this);
    lab1->resize(30,30);
    lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
    lab1->setScaledContents(true);
    lab1->move(edit1->x()-40,edit1->y());

    QLabel *lab2=new QLabel(this);
    lab2->resize(lab1->size());
    lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
    lab2->setScaledContents(true);
    lab2->move(edit2->x()-40,edit2->y());

    QLabel *lab3=new QLabel(this);
    lab3->resize(400,120);
    lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
    lab3->setScaledContents(true);

    connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
    zh=edit2;
    mm=edit1;
    //设置数据库
    if(!db.contains("mydb.db"))
    {
        db=QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("mydb.db");
    }
    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }
    //创建表
    QString sql="create table if not exists stu_info("
            "zhanghao varchar(20) primary key,"
            "mima varchar(20))";
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","创建表失败");
        return;
    }
}

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

void MainWindow::btn1_slot()
{
    QString i=zh->text();
    QString j=mm->text();
    qDebug()<close();
    }
    return;


}

void MainWindow::btn2_slot()
{
    QMessageBox box(QMessageBox::Question,
                    "退出",
                    "确定要退出吗?",
                    QMessageBox::Yes|QMessageBox::No,
                    this);
    int ret=box.exec();
    if(ret==QMessageBox::Yes)
    {
        this->close();
    }

}

void MainWindow::btn3_slot()
{
    QString zhanghao=zh->text();
    QString mima=mm->text();
    if(zhanghao.isEmpty()||mima.isEmpty())
    {
        QMessageBox::information(this,"提示","信息不完整");
        return;
    }
    QString sql=QString("insert into stu_info(zhanghao,mima)"
                        "values('%1','%2')").arg(zhanghao).arg(mima);
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","添加失败");
        return;
    }else
    {
        QMessageBox::information(this,"成功","添加成功");
    }
}

效果图

添加

QT--day5_第1张图片

登录

QT--day5_第2张图片

QT--day5_第3张图片

你可能感兴趣的:(qt,开发语言)