当前位置:   article > 正文

QT5制作简易登录程序_qt5如何使用ssh

qt5如何使用ssh

因为鄙人编程能力不行,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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

用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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

4、制作图标

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

IDI_ICON1 ICON  DISCARDABLE  "myico.ico"
  • 1

在pro文档中添加:

RC_FILE += \
        myico.rc
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/194624
推荐阅读
相关标签
  

闽ICP备14008679号