티스토리 뷰
라즈베리파이 QT C++ SQLite 데이타베이스 프로그램
1. 로그인, 패스워드 입력하기
2. 데이터 삽입, 업데이트, 삭제하기 (콤보박스, 리스트뷰, 트리뷰에서 선택하면 에디트 박스에 보이기)
3. 위에서 삽입, 업데이트, 삭제한 데이터베이스가 SQLite
4. 라즈베리파이 QT C++ 이용
5. 전체 소스 파일 구성
6. 로그인 폼 mainwindow.ui
7. 위의 로그인 폼 mainwindow.ui를 다음과 같은 컨트롤를 이용하여 꾸며준다.
8. 데이터 입력,업데이트,삭제 폼 employeeinfo.ui
9. 위의 employeeinfo.ui 폼을 다음과 같은 컨트롤로 구성한다.
10. 소스코드를 작성한다.
11. Login.pro
SQLite 데이터베이스 사용을 위해 sql만 입력해준다.
#-------------------------------------------------
#
# 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 \
employeeinfo.cpp
HEADERS += mainwindow.h \
employeeinfo.h
FORMS += mainwindow.ui \
employeeinfo.ui
12. employeeinfo.h
employeeinfo.h와 employeeinfo.cpp 파일은 새롭게 추가한 파일이다.
File -> New File or Project ->Qt -> Qt Designer Form Class -> Dialgo without Buttons
기존에 있던 mainwindow.h 파일을 인클루드해준다.
그리고 나머지 추가된 소스파일은 각 컨트롤에서 Go to slot으로 자동으로 만들어진 소스코드이다.
#ifndef EMPLOYEEINFO_H
#define EMPLOYEEINFO_H
#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class Employeeinfo;
}
class Employeeinfo : public QDialog
{
Q_OBJECT
public:
explicit Employeeinfo(QWidget *parent = 0);
~Employeeinfo();
private slots:
void on_pushButton_save_clicked();
void on_pushButton_Edit_clicked();
void on_pushButton_delete_clicked();
void on_pushButton_load_tbl_clicked();
void on_pushButton_load_list_clicked();
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_tableView_activated(const QModelIndex &index);
void on_listView_activated(const QModelIndex &index);
private:
Ui::Employeeinfo *ui;
};
#endif // EMPLOYEEINFO_H
13. mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QDebug>
#include <QFileInfo>
#include "employeeinfo.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QSqlDatabase mydb;
void connClose()
{
mydb.close();
mydb.removeDatabase(QSqlDatabase::defaultConnection);
}
bool connOpen()
{
mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("/home/pi/database.db");
if(!mydb.open())
{
qDebug()<<("Failed to open the databases");
return false;
}
else
{
qDebug()<<("Connected...");
return true;
}
}
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
14. employeeinfo.cpp
employeeinfo.h와 employeeinfo.cpp 파일은 새롭게 추가한 파일이다.
File -> New File or Project ->Qt -> Qt Designer Form Class -> Dialgo without Buttons
#include "employeeinfo.h"
#include "ui_employeeinfo.h"
#include <QMessageBox>
Employeeinfo::Employeeinfo(QWidget *parent) :
QDialog(parent),
ui(new Ui::Employeeinfo)
{
ui->setupUi(this);
MainWindow conn;
if(!conn.connOpen())
ui->label_sec_status->setText("Failed to open the databases");
else
ui->label_sec_status->setText("Connected...");
}
Employeeinfo::~Employeeinfo()
{
delete ui;
}
void Employeeinfo::on_pushButton_save_clicked()
{
MainWindow conn;
QString eid,name,surname,age;
eid = ui->lineEdit_eid->text();
name= ui->lineEdit_name->text();
surname = ui->lineEdit_surname->text();
age = ui->lineEdit_age->text();
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("insert into employeeinfo (eid,name,surname,age) values('"+eid+"','"+name+"','"+surname+"','"+age+"')");
if(qry.exec())
{
QMessageBox::information(this,tr("Save"),tr("Saved"));
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
void Employeeinfo::on_pushButton_Edit_clicked()
{
MainWindow conn;
QString eid,name,surname,age;
eid = ui->lineEdit_eid->text();
name= ui->lineEdit_name->text();
surname = ui->lineEdit_surname->text();
age = ui->lineEdit_age->text();
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("update employeeinfo set eid='"+eid+"',name='"+name+"',surname='"+surname+"',age='"+age+"' where eid = '"+eid+"'");
if(qry.exec())
{
QMessageBox::information(this,tr("Edit"),tr("Updated"));
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
void Employeeinfo::on_pushButton_delete_clicked()
{
MainWindow conn;
QString eid,name,surname,age;
eid = ui->lineEdit_eid->text();
// name= ui->lineEdit_name->text();
//surname = ui->lineEdit_surname->text();
// age = ui->lineEdit_age->text();
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("Delete from employeeinfo where eid = '"+eid+"'");
if(qry.exec())
{
QMessageBox::information(this,tr("Delete"),tr("Deleted"));
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
void Employeeinfo::on_pushButton_load_tbl_clicked()
{
MainWindow conn;
QSqlQueryModel * modal = new QSqlQueryModel();
conn.connOpen();
QSqlQuery* qry = new QSqlQuery(conn.mydb);
//qry->prepare("select * from employeeinfo");
qry->prepare("select eid, name, surname from employeeinfo");
qry->exec();
modal->setQuery(*qry);
ui->tableView->setModel(modal);
conn.connClose();
qDebug() << (modal->rowCount());
}
void Employeeinfo::on_pushButton_load_list_clicked()
{
MainWindow conn;
QSqlQueryModel * modal = new QSqlQueryModel();
conn.connOpen();
QSqlQuery* qry = new QSqlQuery(conn.mydb);
//qry->prepare("select * from employeeinfo");
qry->prepare("select name from employeeinfo");
qry->exec();
modal->setQuery(*qry);
ui->listView->setModel(modal);
ui->comboBox->setModel(modal);
conn.connClose();
qDebug() << (modal->rowCount());
}
void Employeeinfo::on_comboBox_currentIndexChanged(const QString &arg1)
{
QString name = ui->comboBox->currentText();
MainWindow conn;
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("select * from employeeinfo where name = '"+name+"'");
if(qry.exec())
{
while(qry.next())
{
ui->lineEdit_eid->setText(qry.value(0).toString());
ui->lineEdit_name->setText(qry.value(1).toString());
ui->lineEdit_surname->setText(qry.value(2).toString());
ui->lineEdit_age->setText(qry.value(3).toString());
}
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
void Employeeinfo::on_tableView_activated(const QModelIndex &index)
{
QString val = ui->tableView->model()->data(index).toString();
MainWindow conn;
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("select * from employeeinfo where eid = '"+val+"' or name ='"+val+"'or surname ='"+val+"'or age ='"+val+"'");
if(qry.exec())
{
while(qry.next())
{
ui->lineEdit_eid->setText(qry.value(0).toString());
ui->lineEdit_name->setText(qry.value(1).toString());
ui->lineEdit_surname->setText(qry.value(2).toString());
ui->lineEdit_age->setText(qry.value(3).toString());
}
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
void Employeeinfo::on_listView_activated(const QModelIndex &index)
{
QString val = ui->listView->model()->data(index).toString();
MainWindow conn;
if(!conn.connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("select * from employeeinfo where eid = '"+val+"' or name ='"+val+"'or surname ='"+val+"'or age ='"+val+"'");
if(qry.exec())
{
while(qry.next())
{
ui->lineEdit_eid->setText(qry.value(0).toString());
ui->lineEdit_name->setText(qry.value(1).toString());
ui->lineEdit_surname->setText(qry.value(2).toString());
ui->lineEdit_age->setText(qry.value(3).toString());
}
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error::"),qry.lastError().text());
}
}
15.main.cpp
건드린게 없다.
16. mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("/home/pi/Pictures/apple.png");
ui->label_pic->setPixmap(pix);
// mydb = QSqlDatabase::addDatabase("QSQLITE");
// mydb.setDatabaseName("/home/pi/database.db");
if(!connOpen())
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(!connOpen())
{
qDebug() <<"Failed to open the database";
return;
}
connOpen();
QSqlQuery qry;
qry.prepare("select * from employeeinfo where username = '"+username+"'and password='"+password+"'");
if(qry.exec())
{
int count=0;
while(qry.next())
{
count++;
}
if(count==1)
{
ui->label->setText("username and password is correct");
connClose();
this->hide();
Employeeinfo employeeinfo;
employeeinfo.setModal(true);
employeeinfo.exec();
}
if(count>1)
ui->label->setText("Duplicate username and password");
if(count<1)
ui->label->setText("username and password is not correct");
}
}
16. 프로그램 실행 영상 보기
'프로그래밍 > 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- Login Form using sqlite in QT application (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 |