This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
134 lines (113 loc) · 3.35 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QSettings>
//#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tabel = QSqlDatabase::addDatabase("QSQLITE");
//QSettings tatanan(QSettings::IniFormat, QSettings::UserScope, "Baddwin", "AdolPulza");
#ifdef Q_OS_WIN
QString tabelFile = "adolpulza.sqlite";
#else
QString tabelFile = "adolpulza.sqlite";
#endif
QFileInfo infoTabel(tabelFile);
if(infoTabel.exists())
{
if(infoTabel.isFile())
{
tabel.setDatabaseName(tabelFile);
if(!tabel.open())
{
ui->statusBar->showMessage(tabel.lastError().text());
//return 0;
}
else
{
ui->statusBar->showMessage("Database berhasil dibuka",3000);
}
//qDebug() << "Database siap gan...";
}
}
else
{
//qDebug() << "Ada kesalahan";
ui->statusBar->showMessage("File tidak ditemukan");
}
model = new QSqlTableModel(parent,tabel);
model->setTable("laporan");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
//model->database().transaction();
// model->setHeaderData(0,Qt::Horizontal,tr("No"));
// model->setHeaderData(1,Qt::Horizontal,tr("Tanggal"));
// model->setHeaderData(2,Qt::Horizontal,tr("Nomer"));
// model->setHeaderData(3,Qt::Horizontal,tr("Biaya"));
// model->setHeaderData(4,Qt::Horizontal,tr("Keterangan"));
ui->tableView->setModel(model);
//coba dengan qsqlquerymodel
// QSqlQueryModel *model = new QSqlQueryModel(parent);
// model->setQuery("SELECT * FROM laporan",tabel);
// ui->tableView->setModel(model);
ui->tableView->resizeColumnToContents(0);
ui->tableView->setColumnWidth(1,100);
ui->tableView->setColumnWidth(2,200);
ui->tableView->horizontalHeader()->setStretchLastSection(true);
ui->tableView->verticalHeader()->hide();
//ui->tableView->show();
connect(ui->actionSave,SIGNAL(triggered()),this,SLOT(simpan()));
connect(ui->actionReset,SIGNAL(triggered()),this,SLOT(takjadi()));
}
MainWindow::~MainWindow()
{
delete ui;
tabel.close();
}
void MainWindow::on_actionQuit_triggered()
{
qApp->quit();
}
void MainWindow::on_actionAbout_Qt_triggered()
{
qApp->aboutQt();
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox *about = new QMessageBox(this);
about->setWindowTitle("Tentang Program");
about->setText("Adol Pulza\n2014 Slamet Badwi");
about->setIcon(QMessageBox::Information);
about->exec();
}
void MainWindow::simpan()
{
if(model->submitAll())
{
ui->statusBar->showMessage("Berhasil disimpan",10000);
}
else
{
ui->statusBar->showMessage("Tidak bisa menyimpan: "+tabel.lastError().text(),5000);
}
}
void MainWindow::takjadi()
{
model->revertAll();
ui->statusBar->showMessage("Tabel dikembalikan pada kondisi awal",5000);
}
void MainWindow::on_actionAdd_triggered()
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount()+1,0),model->rowCount()+1);
}
void MainWindow::on_actionDelete_triggered()
{
model->removeRow(ui->tableView->currentIndex().row());
}