티스토리 뷰

QT C++ GUI Tutorial  Login Form using sqlite in QT application


#-------------------------------------------------

#

# Project created by QtCreator 2018-09-27T10:03:21

#

#-------------------------------------------------


QT       += core gui sql


greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = Login

TEMPLATE = app


# The following define makes your compiler emit warnings if you use

# any feature of Qt which as been marked as 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 you use 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



#ifndef MAINWINDOW_H

#define MAINWINDOW_H


#include <QMainWindow>

#include <QtSql>

#include <QDebug>

#include <QFileInfo>



namespace Ui {

class MainWindow;

}


class MainWindow : public QMainWindow

{

    Q_OBJECT


public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();


private slots:

    void on_pushButton_clicked();


private:

    Ui::MainWindow *ui;

    QSqlDatabase mydb;

};


#endif // MAINWINDOW_H



#include "mainwindow.h"

#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);


    mydb = QSqlDatabase::addDatabase("QSQLITE");

    mydb.setDatabaseName("/home/pi/database.db");


    if(!mydb.open())

        ui->label->setText("Failed to open the databases");

    else

        ui->label->setText("Connected...");

}


MainWindow::~MainWindow()

{

    delete ui;

}


void MainWindow::on_pushButton_clicked()

{

    QString username, password;

    username = ui->lineEdit_username->text();

    password= ui->lineEdit_password->text();


    if(!mydb.isOpen())

    {

        qDebug() <<"Failed to open the database";

        return;

    }


    QSqlQuery qry;


    if(qry.exec("select * from employeeinfo where username = '"+username+"'and password='"+password+"'"))

    {

        int count=0;

        while(qry.next())

        {

            count++;

        }

        if(count==1)

            ui->label->setText("username and password is correct");

        if(count>1)

            ui->label->setText("Duplicate username and password");

        if(count<1)

            ui->label->setText("username and password is not correct");

    }

}




댓글
최근에 달린 댓글
글 보관함
«   2024/04   »
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
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글