The login and registration interface designed by Qt Creater uses SQLite database
Case screenshot
Login page
Registration page
Screenshot of project directory structure
Code
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) {<!-- --> QApplication a(argc, argv); MainWindow w; //The first one is to remove the original border and title bar, the second one is to retain the minimize and restore functions, mainly for restoration, the minimize is implemented below w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); w.show(); return a.exec(); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "signup.h" #include <QGraphicsDropShadowEffect> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) {<!-- --> ui->setupUi(this); //Set the picture QPixmap *pix = new QPixmap(":/images/blue.png"); QSize sz = ui->label_image->size(); ui->label_image->setPixmap(pix->scaled(sz)); //Set the image shadow effect QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this); shadow->setOffset(-3, 0); shadow->setColor(QColor(136,136,136)); shadow->setBlurRadius(30); ui->label_image->setGraphicsEffect(shadow); } MainWindow::~MainWindow() {<!-- --> delete ui; } void sqlite_Init() {<!-- --> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("user.db"); if(!db.open()) {<!-- --> qDebug()<<"open error"; } //create excle QString createsql=QString("create table if not exists user(id integer primary key autoincrement," "username ntext unique not NULL," "password ntext not NULL)"); QSqlQuery query; if(!query.exec(createsql)){<!-- --> qDebug()<<"table create error"; } else{<!-- --> qDebug()<<"table create success"; } } void MainWindow::on_btn_signin_clicked() {<!-- --> sqlite_Init(); QString username = ui->lineEdit_username->text(); QString password = ui->lineEdit_password->text(); QString sql=QString("select * from user where username='%1' and password='%2'").arg(username,password); //Create execution statement object QSqlQuery query(sql); //Judge the execution result if(!query.next()) {<!-- --> qDebug()<<"Login error"; QMessageBox::information(this,"Login authentication","Login failed, account or password is wrong"); } else {<!-- --> qDebug()<<"Login success"; QMessageBox::information(this,"Login authentication","Login successful"); //You can jump to other pages after successful login QWidget *w = new QWidget; w->show(); this->close(); } } void MainWindow::on_btn_signup_clicked() {<!-- --> this->close(); Signup *s = new Signup; s->show(); } void MainWindow::on_btn_close_clicked() {<!-- --> this->close(); }
signup.cpp
#include "signup.h" #include "ui_signup.h" #include "mainwindow.h" Signup::Signup(QWidget *parent) : QWidget(parent), ui(new Ui::Signup) {<!-- --> ui->setupUi(this); //Set the picture QPixmap *pix = new QPixmap(":/images/women.png"); QSize sz = ui->label_image->size(); ui->label_image->setPixmap(pix->scaled(sz)); } Signup::~Signup() {<!-- --> delete ui; } void Signup::on_btn_return_clicked() {<!-- --> MainWindow *w = new MainWindow; w->show(); this->close(); } void Signup::on_btn_sure_clicked() {<!-- --> sqlite_Init(); QString username = ui->lineEdit_username->text(); QString password = ui->lineEdit_passwd->text(); QString surepass = ui->lineEdit_surepasswd->text(); //Determine whether the password is consistent if(password == surepass) {<!-- --> QString sql=QString("insert into user(username,password) values('%1','%2');") .arg(username).arg(password); //Create execution statement object QSqlQuery query; //Judge the execution result if(!query.exec(sql)) {<!-- --> qDebug()<<"insert into error"; QMessageBox::information(this,"Registration Authentication","Insertion failed!"); } else {<!-- --> qDebug()<<"insert into success"; QMessageBox::information(this,"Registration Authentication","Insertion successful!"); MainWindow *w = new MainWindow; w->show(); this->close(); } }else{<!-- --> QMessageBox::information(this,"Registration Authentication","Two password inputs are inconsistent"); } }
Page UI
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>780</width> <height>520</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(255, 255, 255);</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="label_image"> <property name="geometry"> <rect> <x>385</x> <y>10</y> <width>380</width> <height>500</height> </rect> </property> <property name="text"> <string/> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>200</width> <height>20</height> </rect> </property> <property name="text"> <string>Switch page</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>60</x> <y>80</y> <width>200</width> <height>40</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 87 20pt & amp;quot;imitating Song Dynasty & amp;quot;;</string> </property> <property name="text"> <string>Log in now</string> </property> </widget> <widget class="QPushButton" name="btn_signin"> <property name="geometry"> <rect> <x>70</x> <y>400</y> <width>80</width> <height>24</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: qlineargradient(spread:pad, x1:0.52, y1:1, x2:0.54, y2:0, stop:0.0112994 rgba(64, 145, 252, 255), stop:1 rgba(255, 255, 255, 255)); color: rgb(255, 255, 255); border:0px groove gray;border-radius: 7px;padding:2px 4px; font: 14pt & amp;quot;Candara & amp;quot;;</string> </property> <property name="text"> <string>Login</string> </property> </widget> <widget class="QPushButton" name="btn_signup"> <property name="geometry"> <rect> <x>210</x> <y>400</y> <width>80</width> <height>24</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: qlineargradient(spread:pad, x1:0.52, y1:1, x2:0.54, y2:0, stop:0.0112994 rgba(64, 145, 252, 255), stop:1 rgba(255, 255, 255, 255)); color: rgb(255, 255, 255); border:0px groove gray;border-radius: 7px;padding:2px 4px; font: 14pt & amp;quot;Candara & amp;quot;;</string> </property> <property name="text"> <string>Register</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_username"> <property name="geometry"> <rect> <x>60</x> <y>230</y> <width>240</width> <height>40</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(247, 247, 247); border:1px groove gray;border-radius: 7px;padding:2px 4px; font: 10pt & amp;quot;Candara & amp;quot;;</string> </property> <property name="placeholderText"> <string>Enter account number</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_password"> <property name="geometry"> <rect> <x>60</x> <y>300</y> <width>240</width> <height>40</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(247, 247, 247); border:1px groove gray;border-radius: 7px;padding:2px 4px; font: 10pt & amp;quot;Candara & amp;quot;;</string> </property> <property name="placeholderText"> <string>Enter password</string> </property> </widget> <widget class="QPushButton" name="btn_close"> <property name="geometry"> <rect> <x>725</x> <y>15</y> <width>36</width> <height>36</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color:transparent; image: url(:/images/exit.png);</string> </property> <property name="text"> <string/> </property> </widget> </widget> </widget> <resources/> <connections/> </ui>
signup.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Signup</class> <widget class="QWidget" name="Signup"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>790</width> <height>520</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(255, 255, 255);</string> </property> <widget class="QLabel" name="label_image"> <property name="geometry"> <rect> <x>15</x> <y>15</y> <width>380</width> <height>490</height> </rect> </property> <property name="text"> <string/> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>470</x> <y>20</y> <width>131</width> <height>31</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 87 20pt & amp;quot;Arial Black & amp;quot;;</string> </property> <property name="text"> <string>Hello!</string> </property> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>470</x> <y>60</y> <width>261</width> <height>41</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 87 20pt & amp;quot;Arial Black & amp;quot;;</string> </property> <property name="text"> <string>Welcome to join us</string> </property> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>470</x> <y>140</y> <width>131</width> <height>16</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 10pt & amp;quot;Arial & amp;quot;;</string> </property> <property name="text"> <string>Username:</string> </property> </widget> <widget class="QLabel" name="label_5"> <property name="geometry"> <rect> <x>470</x> <y>200</y> <width>131</width> <height>16</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 10pt & amp;quot;Arial & amp;quot;;</string> </property> <property name="text"> <string>Password:</string> </property> </widget> <widget class="QLabel" name="label_6"> <property name="geometry"> <rect> <x>470</x> <y>260</y> <width>131</width> <height>16</height> </rect> </property> <property name="styleSheet"> <string notr="true">font: 10pt & amp;quot;Arial & amp;quot;;</string> </property> <property name="text"> <string>Enter password again:</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_username"> <property name="geometry"> <rect> <x>470</x> <y>160</y> <width>200</width> <height>30</height> </rect> </property> </widget> <widget class="QLineEdit" name="lineEdit_passwd"> <property name="geometry"> <rect> <x>470</x> <y>220</y> <width>200</width> <height>30</height> </rect> </property> </widget> <widget class="QLineEdit" name="lineEdit_surepasswd"> <property name="geometry"> <rect> <x>470</x> <y>290</y> <width>200</width> <height>30</height> </rect> </property> </widget> <widget class="QPushButton" name="btn_sure"> <property name="geometry"> <rect> <x>470</x> <y>350</y> <width>111</width> <height>24</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(29, 123, 255); color: rgb(255, 255, 255); font: 25 9pt & amp;quot;Bahnschrift Light & amp;quot;;</string> </property> <property name="text"> <string>OK</string> </property> </widget> <widget class="QPushButton" name="btn_return"> <property name="geometry"> <rect> <x>470</x> <y>400</y> <width>111</width> <height>24</height> </rect> </property> <property name="styleSheet"> <string notr="true">background-color: rgb(29, 123, 255); color: rgb(255, 255, 255); font: 25 9pt & amp;quot;Bahnschrift Light & amp;quot;;</string> </property> <property name="text"> <string>Return to login</string> </property> </widget> </widget> <resources/> <connections/> </ui>
Header file
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSqlDatabase> #include <QSqlQuery> #include <QMessageBox> #include <QDebug> void sqlite_Init(); QT_BEGIN_NAMESPACE namespace Ui {<!-- --> class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow {<!-- --> Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_btn_signin_clicked(); void on_btn_signup_clicked(); void on_btn_close_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
signup.h
#ifndef SIGNUP_H #define SIGNUP_H #include <QWidget> namespace Ui {<!-- --> class Signup; } class Signup : public QWidget {<!-- --> Q_OBJECT public: explicit Signup(QWidget *parent = nullptr); ~Signup(); private slots: void on_btn_return_clicked(); void on_btn_sure_clicked(); private: Ui::Signup *ui; }; #endif // SIGNUP_H
Project file Login.pro
QT + = core gui sql greaterThan(QT_MAJOR_VERSION, 4): QT + = widgets CONFIG+=c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES + = QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES + = \ main.cpp \ mainwindow.cpp \ signup.cpp HEADERS + = \ mainwindow.h \ signup.h FORMS + = \ mainwindow.ui \ signup.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS + = target RESOURCES + = \ images.qrc
Image resources
button
Registration page image