QT實現(xiàn)用戶登錄注冊
更新時間:2022年06月14日 14:43:27 作者:阿寧(xin)。
這篇文章主要為大家詳細介紹了QT實現(xiàn)用戶登錄注冊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了QT實現(xiàn)用戶登錄注冊的具體代碼,供大家參考,具體內(nèi)容如下
#include "widget.h"
#include "ui_widget.h"
//窗口設(shè)置
#include <QStyle>
//數(shù)據(jù)庫連接
#include <QtSql/QSqlQuery>//數(shù)據(jù)庫操作函數(shù)
#include <QtSql/QSqlError>//輸出錯誤信息
#include <QMessageBox>//
#include <QSettings>//讀寫配置文件
#include <QtDebug>
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
? ? , ui(new Ui::Widget)
{
? ? ui->setupUi(this);
//窗口設(shè)置
? ? //設(shè)置窗口不顯示標題,無邊框
? ? ?setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
//設(shè)置最小化、關(guān)閉按鈕
? ? ?//獲取最小化、關(guān)閉按鈕圖標
? ? ? QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
? ? ? QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
? ? ? ui->mintoolButton->setIcon(minPix);
? ? ? ui->closetoolButton->setIcon(closePix);
? ? ? ui->mintoolButton->setStyleSheet("bakground-color:tranparent:");
? ? ? ui->closetoolButton->setStyleSheet("bakground-color:tranparent:");
? ? ? connect(ui->mintoolButton,&QPushButton::clicked,this,&Widget::showMinimized);
? ? ? connect(ui->closetoolButton,&QPushButton::clicked,this,&Widget::close);
//數(shù)據(jù)庫連接
? ? ? //連接數(shù)據(jù)庫
? ? ? ? ?//查看當前支持的數(shù)據(jù)庫的驅(qū)動
? ? ? ? ? qDebug()<<QSqlDatabase::drivers();
? ? ? ? ? QSqlDatabase DB;//創(chuàng)建一個數(shù)據(jù)庫的文件
? ? ? ? ?//加載數(shù)據(jù)庫的文件
? ? ? ? ? QString aFile="./dataBase.db";
? ? ? ? ? if(aFile.isEmpty())
? ? ? ? ? {
? ? ? ? ? ? ? qDebug()<<" 數(shù)據(jù)庫文件加載失敗 " ;
? ? ? ? ? ? ? return ;
? ? ? ? ? }
? ? ? ? ? //打開數(shù)據(jù)庫
? ? ? ? ? DB=QSqlDatabase::addDatabase("QSQLITE");//創(chuàng)建QSQLITE數(shù)據(jù)庫連接
? ? ? ? ? DB.setDatabaseName(aFile); //數(shù)據(jù)庫名
? ? ? ? ? if(!DB.open())
? ? ? ? ? {
? ? ? ? ? ? ? //沒有數(shù)據(jù)庫文件則創(chuàng)建文件
? ? ? ? ? ? ? qDebug()<<"數(shù)據(jù)庫文件打開失敗";
? ? ? ? ? ? ? qDebug()<<DB.lastError().text();//輸出錯誤信息
? ? ? ? ? ?}
? ? ? ? ? qDebug()<< " 打開數(shù)據(jù)庫文件成功 " ;
? ? ? //從配置文件中讀取用戶名和密碼:
? ? ? ? ? QSettings setting("config.ini",QSettings::IniFormat);
? ? ? ? ? QString account = setting.value("section/account").toString();
? ? ? ? ? QString password = setting.value("section/password").toString();
? ? ? ? ? ui->accountEdit->setText(account);
? ? ? ? ? ui->passwordEdit->setText(password);
? ? ? ? ?//用戶注冊,向數(shù)據(jù)庫插入數(shù)據(jù)
? ? ? ? ? connect(ui->registerButton,&QPushButton::clicked,this,[=](){
? ? ? ? ? //接受用戶輸入:
? ? ? ? ? ? ? QString account = ui->accountEdit->text();//用戶賬號
? ? ? ? ? ? ? QString password = ui->passwordEdit->text();//密碼
? ? ? ? ? //創(chuàng)建自己需要使用的表格
? ? ? ? ? ? ? QSqlQuery query(DB);
? ? ? ? ? ? ? if(!query.exec("create table user(account varchar(255) primary key, password varchar(255))"))
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? qDebug() << "Error: Fail to create table."<< query.lastError();
? ? ? ? ? ? ? }
? ? ? ? ? ? ? else
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? qDebug() << "Table created!";
? ? ? ? ? ? ? }
? ? ? ? ? //插入數(shù)據(jù)
? ? ? ? ? ? ? QString qs = QString("insert into user(account,password) values('%1','%2')")
? ? ? ? ? ? ? ? ? ? ? .arg(account).arg(password);
? ? ? ? ? ? ? if(query.exec(qs)) //如果插入成功
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? QMessageBox::information(this,"注冊","注冊成功");
? ? ? ? ? ? ? }
? ? ? ? ? ? ? else
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? QMessageBox::information(this,"注冊","注冊失敗");
? ? ? ? ? ? ? ? ? qDebug()<<query.lastError().text();//輸出錯誤信息
? ? ? ? ? ? ? ? ? //return;
? ? ? ? ? ? ? }
? ? ? ? ? //查看數(shù)據(jù)
? ? ? ? ? ? ? //查看數(shù)據(jù)庫中有的表格的名字
? ? ? ? ? ? ? qDebug()<<"查看數(shù)據(jù)庫中所有的表:";
? ? ? ? ? ? ? QStringList str_table=DB.tables();
? ? ? ? ? ? ? qDebug()<<str_table;
? ? ? ? ? ? ? //查詢數(shù)據(jù)庫中的數(shù)據(jù)
? ? ? ? ? ? ? qDebug()<<"查看數(shù)據(jù)庫中數(shù)據(jù)";
? ? ? ? ? ? ? query.prepare ("SELECT * FROM user");
? ? ? ? ? ? ? query.exec();
? ? ? ? ? ? ? while(query.next())
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? qDebug()<<QString("account:%1,password:%2").
? ? ? ? ? ? ? ? ? ? ? ? ? ? arg(query.value("account").toString()).arg(query.value("password").toString());
? ? ? ? ? ? ? }
? ? ? ? ? });
? ? ? //用戶登錄:查詢數(shù)據(jù)
? ? ? ? ? connect(ui->loginButton,&QPushButton::clicked,this,[=](){
? ? ? ? ? ? ? //接受用戶輸入:
? ? ? ? ? ? ? ?QString account = ui->accountEdit->text();//用戶賬號
? ? ? ? ? ? ? ?QString password = ui->passwordEdit->text();//密碼
? ? ? ? ? ? ? //查詢數(shù)據(jù)
? ? ? ? ? ? ? ?QSqlQuery query;//操作數(shù)據(jù)庫
? ? ? ? ? ? ? ?QString qs = QString("select * from user where account ='%1' and password='%2'").
? ? ? ? ? ? ? ? ? ? ? ?arg(account).arg(password);
? ? ? ? ? ? ? ?if(!query.exec(qs))//如果沒有查到記錄
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?qDebug() << query.lastError().text();//輸出錯誤信息
? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?//獲取查詢的數(shù)據(jù)
? ? ? ? ? ? ? ?if(query.next())//獲取到數(shù)據(jù)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?QMessageBox::information(this,"登錄","登錄成功");
? ? ? ? ? ? ? ? ? ?connect(tw,&TestWidget::testSigna,this,&Widget::show);//顯示主窗口
? ? ? ? ? ? ? ? ? ?//在配置文件中記錄用戶賬號密碼
? ? ? ? ? ? ? ? ? ?if(ui->checkBox->isChecked())//選中,也就是用戶已經(jīng)登錄
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?//創(chuàng)建配置文件
? ? ? ? ? ? ? ? ? ? ? ?QSettings setting("config.ini",QSettings::IniFormat);//配置文件在工程目錄下
? ? ? ? ? ? ? ? ? ? ? ?//把用戶賬號密碼寫到配置文件中
? ? ? ? ? ? ? ? ? ? ? ?setting.beginGroup("section");//節(jié)開始
? ? ? ? ? ? ? ? ? ? ? ?setting.setValue("account",account);
? ? ? ? ? ? ? ? ? ? ? ?setting.setValue("password",password);
? ? ? ? ? ? ? ? ? ? ? ?setting.endGroup();//結(jié)束
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?QMessageBox::information(this,"登錄","登錄失敗");
? ? ? ? ? ? ? ?}
? ? ? ? ? });
}
Widget::~Widget()
{
? ? delete ui;
}#按鈕的ui實現(xiàn)


結(jié)果



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 17轉(zhuǎn)發(fā)一個函數(shù)調(diào)用的完美實現(xiàn)
這篇文章主要給大家介紹了關(guān)于C++ 17如何轉(zhuǎn)發(fā)一個函數(shù)調(diào)用的完美實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C++17具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
C語言嵌入式實現(xiàn)支持浮點輸出的printf示例詳解
這篇文章主要為大家介紹了C語言嵌入式實現(xiàn)支持浮點輸出的printf示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

