-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevicemodel.cpp
94 lines (83 loc) · 2.32 KB
/
devicemodel.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
#include "devicemodel.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
DeviceModel::DeviceModel(QObject *parent, const QSqlDatabase &db) :
QSqlQueryModel(parent),
db_(db)
{
}
bool DeviceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool ok = false;
if(index.column() < 1 || index.column() > 3)
{
return false;
}
// 获patientId对应的QModelIndex
QModelIndex primaryIndex = QSqlQueryModel::index(index.row(),0);
qDebug() << primaryIndex;
// 获取对应行的patientId的值
int devId = data(primaryIndex).toInt();
if (index.column() == 1)
{
ok = this->setDerial(devId, value.toString());
}
if (index.column() == 2)
{
ok = this->setRefresh(devId, value.toString());
}
// if (index.column() == 3)
// {
// ok = this->setAge(patientId, value.toInt());
// }
if (ok)
{
this->setQuery("select * from device;");
}
else
{
qDebug() << "sql fail:";
}
return ok;
}
// 1、2、3列可编辑
Qt::ItemFlags DeviceModel::flags(const QModelIndex &index) const
{
// 获取index所在的flags
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1 || index.column() == 2 || index.column() == 3)
{
flags |= Qt::ItemIsEditable;
}
return flags;
}
bool DeviceModel::setDerial(int id, QString derial)
{
QSqlQuery query(db_);
query.prepare("update device set dev_derial=:derial where dev_id=:id;");
query.bindValue(":dev_derial", derial);
query.bindValue(":id", id);
// if (!query.exec())
// {
// qDebug() << "error:" << query.lastError().text();
// return false;
// }
return query.exec();
}
bool DeviceModel::setRefresh(int id, QString refresh)
{
QSqlQuery query(db_);
query.prepare("update patient set refresh=:refresh where dev_id=:id;");
query.bindValue(":refresh", refresh);
query.bindValue(":id", id);
return query.exec();
}
//bool PatientModel::setAge(int id, int age)
//{
// QSqlQuery query(db_);
// query.prepare("update patient set age=:age where patientId=:id;");
// query.bindValue(":age", age);
// query.bindValue(":id", id);
// return query.exec();
//}