QT5制作简易登录程序

因为鄙人编程能力不行,C++成绩都是飘过,可能程序存在不少BUG和缺陷,界面也不太美观哈,毕竟不是美院的哈哈,请大家自己拿去改善。

我们今天来做一个简易的登录程序,如图所示:
这里写图片描述
当然这也不只是登录程序那么简单,我们需要在SQLite数据库中获取用户名和密码,你可以在pro文件下自己用SQLite语句创建表格,也可以在QT creator中创建。我是直接在QT creator中创建并删除掉了。

1、设计ui

创建widgets的dialog类,在ui文件中如下设计好,并修改对象名称,用的布局也一目了然
这里写图片描述

2、main.cpp

#include "dialog.h"
#include <QApplication>
#include <qdebug.h>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //创建数据库表格
    QSqlDatabase database;
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("test.db");
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else
    {
        qDebug() << "Succeed to connect database." ;
    }

    QSqlQuery sql_query;
    if(!sql_query.exec("create table user(id int primary key, name text, password int)"))
    {
        qDebug() << "Error: Fail to create table."<< sql_query.lastError();
    }
    else
    {
        qDebug() << "Table created!";
    }

    if(!sql_query.exec("INSERT INTO user VALUES(1, \"haha\", 111111)"))
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "inserted haha!";
    }
    if(!sql_query.exec("INSERT INTO user VALUES(2, \"hehe\", 222222)"))
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "inserted hehe!";
    }
    if(!sql_query.exec("INSERT INTO user VALUES(3, \"xixi\", 333333)"))
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "inserted xixi!";
    }

    sql_query.exec("select * from user");
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        while(sql_query.next())
        {
            int id = sql_query.value(0).toInt();
            QString name = sql_query.value(1).toString();
            QString password = sql_query.value(2).toString();
            qDebug()<<QString("id:%1    name:%2    password:%3").arg(id).arg(name).arg(password);
        }
    }
    Dialog w;
    w.show();

    return a.exec();
}

用navicat for sqlite连接显示这样的表格:
这里写图片描述

3、dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
#include <qdebug.h>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setMaximumSize(400,300);
    this->setMinimumSize(400,300);
    this->setWindowTitle("登录程序");
    connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(login_in()));
    connect(ui->buttonBox,SIGNAL(rejected()),this,SLOT(login_out()));
    //设置颜色
    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, Qt::blue);
    this->setAutoFillBackground(true);
    this->setPalette(pal);
}

void Dialog::login_in(void){
    int flag = 0;
    QSqlDatabase database;
    database = QSqlDatabase::database("QSQLITE");
    QSqlQuery sql_query;
    sql_query.exec("select * from user");
    if(!sql_query.exec())
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "output all";
    }
    while(sql_query.next()){
        QString name = sql_query.value(1).toString();
        QString password = sql_query.value(2).toString();
        if(ui->login_edit->text() == name || ui->password_edit->text() == password){
            flag=1;
        }
    }
    if(flag) {
        QMessageBox msg1(QMessageBox::Information,windowTitle(),"登录成功",QMessageBox::Ok,this);
        msg1.exec();
        close();
    }
    else{
        QMessageBox msg2(QMessageBox::Information,windowTitle(),"登录失败",QMessageBox::Ok,this);
        msg2.exec();
    }
    sql_query.exec("drop table user");
    if(sql_query.exec())
    {
        qDebug() << sql_query.lastError();
    }
    else
    {
        qDebug() << "table cleared";
    }
}

void Dialog::login_out(void){
    QMessageBox msg2(QMessageBox::Information,"login","确定退出吗?",QMessageBox::Ok | QMessageBox::Cancel,this);
    if(msg2.exec() == QMessageBox::Ok){
        close();
    }
}
Dialog::~Dialog()
{
    delete ui;
}

4、制作图标

将myico.ico图片复制进pro文件目录下(其他格式的不行),创建文本文档修改为myico.rc文件,利用notepad++对其编辑:

IDI_ICON1 ICON  DISCARDABLE  "myico.ico"

在pro文档中添加:

RC_FILE += \
        myico.rc

猜你喜欢

转载自blog.csdn.net/weixin_41656968/article/details/80502404
Qt5
今日推荐