-
Notifications
You must be signed in to change notification settings - Fork 3
/
financetype.cpp
155 lines (148 loc) · 5.43 KB
/
financetype.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
151
152
153
154
155
#include "financetype.h"
#include "connectsql.h"
#include <QTreeWidgetItem>
#include <QTextCodec>
extern ConnectSql sql;
Financetype::Financetype(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
ui.treeWidget->setColumnCount(1);
ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui.tableWidget->setStyleSheet("QTableWidget{border: 1px solid gray; background-color: transparent; selection-color: grey;}");
ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:white;color: black;padding-left: 4px;border: 1px solid #6c6c6c;};"
"color: white;padding-left: 4px;border: 1px solid #6c6c6c;}"
"QHeaderView::section:checked{background-color: white;color: black;}");
ui.treeWidget->setStyleSheet("QTreeWidget{border: 1px solid gray;color: black; background-color: white;selection-color: grey;}");
//connect(ui.treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(showTable(QTreeWidgetItem*,int)));
initUI();
}
void Financetype::initUI()
{
ui.treeWidget->clear();
ui.tableWidget->setRowCount(0);
QStringList headers;
headers << QString::fromLocal8Bit("财务分类");
ui.treeWidget->setHeaderLabels(headers);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QString strsql;
strsql = "select * from sys_financetype order by id";
QSqlQuery query(*sql.db);
query.exec(strsql);
int row = 0;
while(query.next())
{
QStringList strlist;
strlist <<query.value(2).toString();
QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget, strlist);
ui.treeWidget->addTopLevelItem(item);
ui.tableWidget->insertRow(row);
ui.tableWidget->setItem(row,0,new QTableWidgetItem(query.value(0).toString()));
ui.tableWidget->setItem(row,1,new QTableWidgetItem(query.value(1).toString()));
ui.tableWidget->setItem(row,2,new QTableWidgetItem(query.value(2).toString()));
ui.tableWidget->setItem(row,3,new QTableWidgetItem(query.value(3).toString()));
ui.tableWidget->setItem(row,4,new QTableWidgetItem(query.value(4).toString()));
row++;
}
ui.treeWidget->expandAll();
ui.saveButton->setEnabled(false);
//ui.deleteButton->setEnabled(false);
}
void Financetype::showTable(QTreeWidgetItem*item, int column)
{
ui.tableWidget->setRowCount(0);
QString strText = item->text(0);
QString strsql;
strsql = "select * from sys_financetype where name = '"+strText+"' order by id";
QSqlQuery query(*sql.db);
query.exec(strsql);
int row = 0;
while(query.next())
{
ui.tableWidget->insertRow(row);
ui.tableWidget->setItem(row,0,new QTableWidgetItem(query.value(0).toString()));
ui.tableWidget->setItem(row,1,new QTableWidgetItem(query.value(1).toString()));
ui.tableWidget->setItem(row,2,new QTableWidgetItem(query.value(2).toString()));
ui.tableWidget->setItem(row,3,new QTableWidgetItem(query.value(3).toString()));
ui.tableWidget->setItem(row,4,new QTableWidgetItem(query.value(4).toString()));
row++;
}
}
void Financetype::on_addButton_clicked()
{
initUI();
ui.saveButton->setEnabled(true);
ui.tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
int row = ui.tableWidget->rowCount();
ui.tableWidget->insertRow(row);
ui.tableWidget->setItem(row,0,new QTableWidgetItem(QString::number(row+1)));
}
void Financetype::on_editButton_clicked()
{
ui.tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
ui.saveButton->setEnabled(true);
ui.editButton->setEnabled(true);
ui.deleteButton->setEnabled(true);
}
void Financetype::on_saveButton_clicked()
{
QSqlQuery query(*sql.db);
QString strsql = "delete from sys_financetype";
query.exec(strsql);
int row = ui.tableWidget->model()->rowCount();
for (int i= 0;i<row;i++)
{
query.prepare("INSERT INTO sys_financetype VALUES (?,?, ?, ?, ?)");
query.bindValue(0, ui.tableWidget->item(i,0)->text().toInt());
for (int j=1;j<5;j++)
{
if (ui.tableWidget->item(i,j)==NULL)
query.bindValue(j, NULL);
else
query.bindValue(j, ui.tableWidget->item(i,j)->text());
}
query.exec();
}
initUI();
ui.saveButton->setEnabled(false);
ui.editButton->setEnabled(true);
ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
void Financetype::on_deleteButton_clicked()
{
QMessageBox box(QMessageBox::Warning,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("确认删除?"));
box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
box.setButtonText (QMessageBox::Ok,QString::fromLocal8Bit("确 定"));
box.setButtonText (QMessageBox::Cancel,QString::fromLocal8Bit("取 消"));
if(box.exec()==QMessageBox::Ok)
{
QList<QTableWidgetItem*> list = ui.tableWidget->selectedItems();
if (list.at(0)==NULL)
{
return;
}
QString strName = ui.tableWidget->item(list.at(0)->row(),2)->text();
if (strName==NULL)
{
return;
}
QSqlQuery query(*sql.db);
query.exec("delete from sys_financetype where name = '"+strName+"'");
initUI();
}
}
void Financetype::on_exitButton_clicked()
{
QMessageBox box(QMessageBox::Warning,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("确认退出?"));
box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
box.setButtonText (QMessageBox::Ok,QString::fromLocal8Bit("确 定"));
box.setButtonText (QMessageBox::Cancel,QString::fromLocal8Bit("取 消"));
if(box.exec()==QMessageBox::Ok)
{
this->close();
}
}
Financetype::~Financetype()
{
}