-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadosen.cpp
150 lines (129 loc) · 3.76 KB
/
datadosen.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "datadosen.h"
#include "ui_datadosen.h"
#include<QFileDialog>
#include<QMessageBox>
#include<QSqlDatabase>
#include<QSqlQuery>
#include<QSqlQueryModel>
#include<QSqlError>
#include<QModelIndex>
#include<QStringList>
#include<QAbstractItemModel>
DataDosen::DataDosen(QWidget *parent) :
QDialog(parent),
ui(new Ui::DataDosen)
{
ui->setupUi(this);
db = QSqlDatabase::database();
if(!db.open()){
QMessageBox::warning(this, "Koneksi", db.lastError().text());
return;
}
QSqlQuery* q = new QSqlQuery();
q->prepare("select nip as NIP, nama_dosen as Nama, prodi as Prodi from dosen");
q->exec();
QSqlQueryModel* m = new QSqlQueryModel();
m->setQuery(*q);
ui->tableView->setModel(m);
q->clear();
refresh();
}
DataDosen::~DataDosen()
{
delete ui;
}
void DataDosen::on_pushButton_Close_clicked()
{
accept();
QApplication::activeModalWidget();
mw.raise();
}
void DataDosen::on_pushButton_update_clicked()
{
QString nip = ui->lineEdit_nip->text();
QString nama = ui->lineEdit_nama->text();
QString prodi = ui->lineEdit_prodi->text();
if(!db.open()){
return;
}
QSqlQuery* q = new QSqlQuery();
q->prepare("update dosen set nama_dosen='"+nama+"',prodi='"+prodi+"' where nip='"+nip+"'");
if(q->exec()){
QMessageBox::information(this, "berhasil", "Berhasil diupdate!");
refresh();
clear();
}else{
QMessageBox::warning(this, "Error", "Gagal!");
}
}
void DataDosen::on_pushButton_tambah_clicked()
{
QString nip = ui->lineEdit_nip->text();
QString nama = ui->lineEdit_nama->text();
QString prodi = ui->lineEdit_prodi->text();
if(!db.open()){
return;
}
QSqlQuery* q = new QSqlQuery();
q->prepare("insert into dosen (nip, nama_dosen, prodi) values ('"+nip+"','"+nama+"','"+prodi+"')");
if(q->exec()){
QMessageBox::information(this, "berhasil", "Berhasil ditambahkan");
refresh();
clear();
}else{
QMessageBox::warning(this, "Error", "Data sudah ada!");
}
}
void DataDosen::on_pushButton_delete_clicked()
{
QString nip = ui->lineEdit_nip->text();
QSqlQuery* q = new QSqlQuery();
q->prepare("delete from dosen where nip='"+nip+"'");
if(q->exec()){
QMessageBox::information(this, "Delete", "Berhasil dihapus");
refresh();
clear();
}else{
QMessageBox::warning(this, "Error", q->lastError().text());
}
}
void DataDosen::refresh(){
QSqlQuery* q = new QSqlQuery();
q->prepare("select nip as NIP, nama_dosen as Nama, prodi as Prodi from dosen");
q->exec();
m = new QSqlQueryModel();
m->setQuery(*q);
ui->tableView->setModel(m);
q->clear();
ui->pushButton_tambah->setVisible(true);
ui->pushButton_update->setVisible(false);
ui->pushButton_delete->setVisible(false);
clear();
}
void DataDosen::on_tableView_clicked(const QModelIndex &index)
{
QString nip, nama, prodi;
nip = ui->tableView->model()->index(index.row(), 0).data().toString();
nama = ui->tableView->model()->index(index.row(), 1).data().toString();
prodi = ui->tableView->model()->index(index.row(), 2).data().toString();
ui->lineEdit_nip->setText(nip);
ui->lineEdit_nama->setText(nama);
ui->lineEdit_prodi->setText(prodi);
ui->pushButton_tambah->setVisible(false);
ui->pushButton_update->setVisible(true);
ui->pushButton_delete->setVisible(true);
ui->lineEdit_nip->setDisabled(true);
}
void DataDosen::clear(){
ui->lineEdit_nama->clear();
ui->lineEdit_nip->clear();
ui->lineEdit_prodi->clear();
ui->lineEdit_nip->setDisabled(false);
}
void DataDosen::on_pushButton_clear_clicked()
{
refresh();
}
void DataDosen::on_DataDosen_accepted()
{
}