티스토리 뷰
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");
}
}
'프로그래밍 > Qt' 카테고리의 다른 글
QT C++ GUI Tutorial How to set image with QLabel in Qt (0) | 2018.09.27 |
---|---|
QT C++ GUI Tutorial how to use QLineEdit as password field (0) | 2018.09.27 |
QT C++ GUI Tutorial - How to connect Qt to SQLite (0) | 2018.09.27 |
QT C++ GUI 새로운 다이얼로그박스 만들기 (0) | 2018.09.27 |
QT 메시지 박스 만들기 (0) | 2018.09.27 |