-
Notifications
You must be signed in to change notification settings - Fork 0
/
addcustomer.cpp
119 lines (94 loc) · 3.1 KB
/
addcustomer.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "addcustomer.h"
#include "ui_addcustomer.h"
#include "QtWidgets"
addCustomer::addCustomer(QWidget *parent) :
QDialog(parent),
ui(new Ui::addCustomer)
{
ui->setupUi(this);
centerAndResize();
loadDatabase();
}
void addCustomer::centerAndResize() {
// get the dimension available on this screen
QSize availableSize = qApp->desktop()->availableGeometry().size();
int width = availableSize.width();
int height = availableSize.height();
width *= 0.55; // 35% din rezoultie
height *= 0.55; // 55% din rezolutie
QSize newSize( width, height );
setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
newSize,
qApp->desktop()->availableGeometry()
)
);
}
addCustomer::~addCustomer()
{
delete ui;
}
void addCustomer::on_tableView_activated(const QModelIndex &index)
{
}
void addCustomer::loadDatabase(){
QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE");
QSqlQueryModel *model = new QSqlQueryModel();
m_db.setDatabaseName("C:/Users/Nita/Desktop/Nico2/db.sqlite");
//m_db.setDatabaseName("C:/Users/JohnDoe/Documents/Nico2/db.sqlite");
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok";
}
QSqlQuery *qry = new QSqlQuery();
qry->prepare("select * from customer");
qry->exec();
model->setQuery(*qry);
ui->tableView->setModel(model);
}
void addCustomer::on_saveCustomer_clicked()
{
QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE");
//m_db.setDatabaseName("C:/Users/Nita/Desktop/Nico/db.sqlite");
m_db.setDatabaseName("C:/Users/JohnDoe/Documents/Nico2/db.sqlite");
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok";
}
QString company, cui, bank, iban, country, phone, email, address;
company=ui->companyNameLe->text();
cui = ui->cuiLe->text();
bank = ui->bankLe->text();
iban = ui->ibanLe->text();
country = ui->countryLe->text();
phone = ui->phoneLe->text();
email = ui->emaiLe->text();
address = ui->addressLe->text();
QSqlQuery *qry = new QSqlQuery();
qry->prepare("insert into customer(company, cui, bank, iban, country,phone, email, address) values(:company, :cui, :bank, :iban, :country, :phone, :email, :address)");
qry->bindValue(":company", company);
qry->bindValue(":cui", cui);
qry->bindValue(":bank", bank);
qry->bindValue(":iban", iban);
qry->bindValue(":country", country);
qry->bindValue(":phone", phone);
qry->bindValue(":email", email);
qry->bindValue(":address", address);
if(qry->exec()){
QMessageBox::critical(this, tr("SAVE"),tr("SAVED"));
loadDatabase();
}
else{
QMessageBox::critical(this, tr("error: "), qry->lastError().text());
}
}