-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablemodel.cpp
78 lines (64 loc) · 1.84 KB
/
tablemodel.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
#include "tablemodel.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QModelIndex>
#include <QSqlTableModel>
TableModel::TableModel(QObject *parent) :
QSqlQueryModel(parent)
{
}
QVariant TableModel::data(const QModelIndex &item, int role) const
{
QVariant d = QSqlQueryModel::data(item, role);
QVariant d2 = QSqlQueryModel::data(item,Qt::DisplayRole);
if (role == Qt::TextAlignmentRole)
{
if(d2.type()==QVariant::String)
{
return Qt::AlignLeft;
}
else
{
return Qt::AlignRight;
}
}
if (role == Qt::DisplayRole && d.type() == QVariant::Double)
{
if(item.column()==1)//KOl
{
return QVariant(QString::number(d.toFloat(), 'f', 3));
}
else
{
return QVariant(QString::number(d.toFloat(), 'f', 2));
}
}
return d;
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.column() != 1)
return false;
int checkId = data(QSqlQueryModel::index(index.row(), 4), Qt::DisplayRole).toInt();
int itemNo = data(QSqlQueryModel::index(index.row(), 5), Qt::DisplayRole).toInt();
bool ok = setAmount(checkId, itemNo, value.toDouble());
emit dataChanged(index, index);
return ok;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1)
flags |= Qt::ItemIsEditable;
return flags;
}
bool TableModel::setAmount(int checkId, int itemNo, const double newAmount)
{
QSqlQuery q;
q.prepare("update CheckDetail set kol=:Kol where CheckId=:CheckId and ItemNo=:ItemNo");
q.bindValue(":Kol", newAmount);
q.bindValue(":CheckId", checkId);
q.bindValue(":ItemNo", itemNo);
return q.exec();
}