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


結(jié)果



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 如何利用PyQt5制作一個(gè)簡(jiǎn)單的登錄界面
- 手把手教你實(shí)現(xiàn)漂亮的Qt?登錄界面
- PyQt5實(shí)現(xiàn)用戶登錄GUI界面及登錄后跳轉(zhuǎn)
- pyqt5制作登錄窗口的詳細(xì)過(guò)程
- python通過(guò)PyQt5實(shí)現(xiàn)登錄界面的示例代碼
- PyQt5設(shè)置登錄界面及界面美化的實(shí)現(xiàn)
- PyQt5實(shí)現(xiàn)登錄頁(yè)面
- pyqt5利用pyqtDesigner實(shí)現(xiàn)登錄界面
- pyqt5實(shí)現(xiàn)登錄界面的模板
- QT實(shí)現(xiàn)用戶登錄注冊(cè)功能
相關(guān)文章
C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度
算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小2022-04-04
C語(yǔ)言編寫學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言編寫學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C++語(yǔ)言實(shí)現(xiàn)hash表詳解及實(shí)例代碼
這篇文章主要介紹了C++語(yǔ)言實(shí)現(xiàn)hash表詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
C++面試題之?dāng)?shù)a、b的值互換(不使用中間變量)
這篇文章主要介紹了不使用中間變量,C++實(shí)現(xiàn)數(shù)a、b的值互相轉(zhuǎn)換操作,感興趣的小伙伴們可以參考一下2016-07-07

