qt login interface is simple to make, which is a real nanny level!!!

Posted by Sealr0x on Tue, 18 Jan 2022 04:15:32 +0100

qt login interface is simple to make, which is a real nanny level!!!

Engineering innovation practice in the second week: create a form, design the user login interface (including user name and password), and realize the user login function. When the user submits the login button, he is required to obtain the user name and password data in the interface and match it with the data in the database. If the user name and password are correct, the login is successful, otherwise the login fails.
If you don't want to see it, you can read it directly, but if you can, you can definitely understand it
Link: https://pan.baidu.com/s/1NnnVLxBxThazBQojbft_rw
Extraction code: 33qv

Preliminary preparation (qt creation and database tables)

Start by creating our qt:


Be careful if qmake!

Then keep clicking the next step
Just get such an interface

In the early stage, we need to install a my sql to link our database. In the future, we will have the opportunity to give a tutorial. No, slacker! Ha ha ha ha ha ha ha ha
If you don't want to change the code, you can adjust the code later!!!
We enter our my sql

Input: show databases;

See if you have this

If not, we use to create one

Create a database: create database dbtest;

!!! Be sure to use

Enter the database: use dbtest(Database name);

This line of small characters appears

Use to view tables in the current database: show tables;
We this qt The following table should be used. If not, use it: create table user(username varchar(8),password varchar(8)); 


We also add a little data to it

insert into user(username,password) values('user01','abccba');
insert into user(username,password) values('1','21212121');
Then look at the table: select * from user (Table name);


Like my sql database, I suggest the next software to view it. It's really much more convenient

Page design

Click directly on our ui

Then find the three controls
label

Input box

Button

My layout

Their ui names can be changed in text after being selected

code

Yes Add an sql in pro. Shall we connect to the database
denglu.pro

QT       += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Then we divide it into two parts: connecting to the database and verifying the password h declares our bool connectDB();, Verify that we use a signal slot, so we don't have to declare it
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    bool connectDB();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Our main CPP does not need to be changed or is it the same
main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

Here comes the key part
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include <QDebug>
#include <QSqlQueryModel>
#include <QComboBox>
#include <QSqlQuery>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connectDB();//Call database
    //The trigger of the signal after we click
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){

        QSqlQuery query1;
        query1.exec("SELECT * FROM user WHERE username");//Select our table
        QVariantList userlista;
        QString usert;
        usert=ui->lineEdit->text();//Pass the value of the input box of our ui interface to usert
        QString passwdt;
        passwdt=ui->lineEdit_2->text();//Pass the value of the ui input box to passwdt
        bool T1=false;//Set up judgment signs
        while(query1.next()){//This value traverses the database
        qDebug()<<query1.value(0).toString();//Displayed in qt interface
        qDebug()<<query1.value(1).toString();//Displayed in qt interface
        if(query1.value(0).toString()==usert&&query1.value(1).toString()==passwdt){
           T1=true;//Judge whether the password and account are consistent
        }
        }
        if(T1==true){
            QMessageBox::information(this, "success", "Login successful");
        }
            else { QMessageBox::information(this, "warning", "Wrong user name or password");}
         query1.execBatch();//Cycle again. Without this, it's useless to press your button once
           });

}
bool MainWindow::connectDB(){
   QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
   db.setPort(3306);//Your address
   db.setHostName("localhost");
   db.setUserName("root");//Your my sql account
   db.setPassword("123456");//The password you entered when you went in
   db.setDatabaseName("dbtest"); //Database name

   if(!db.open()){
          QMessageBox::critical(this,"Database open failed",db.lastError().text());
          return false;
      }
      else {
          return true;
      }
   }
MainWindow::~MainWindow()
{
    delete ui;
}

problem

Database problems:

Driver failure (you need to see if you have installed a. dll resource)
Add link description
Comments, private

Database failure (in short, if your my sql is not found, you need to see if your dbtest is available)

Password failure (the password is wrong)

Database host failed (QSqlDatabase db=QSqlDatabase::addDatabase("qmmysql");
db.setPort(3306); This lump of stuff)

Name problem
For example, the name of your input box is not found. After we select it


This is it! Copy this

That's it. Thank you for your love every time

Love you!

Topics: Database MySQL Qt UI