-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoodsmodel.cpp
More file actions
30 lines (25 loc) · 1021 Bytes
/
goodsmodel.cpp
File metadata and controls
30 lines (25 loc) · 1021 Bytes
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
#include "goodsmodel.h"
#include <QColor>
#include <QSqlRecord>
// 1. 构造函数初始化列表这里要改:
GoodsModel::GoodsModel(QObject *parent, QSqlDatabase db)
: QSqlRelationalTableModel(parent, db) // <--- 这里的类名必须和头文件继承的类名一致
{
}
QVariant GoodsModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::BackgroundRole) {
int qtyIndex = fieldIndex("stock_quantity");
int warnIndex = fieldIndex("warning_quantity");
// 简单的安全检查
if (qtyIndex != -1 && warnIndex != -1) {
int currentStock = record(index.row()).value(qtyIndex).toInt();
int warningStock = record(index.row()).value(warnIndex).toInt();
if (currentStock < warningStock) {
return QColor(255, 200, 200);
}
}
}
// 2. 这里也要改,调用新基类的 data 方法:
return QSqlRelationalTableModel::data(index, role); // <--- 改成 QSqlRelationalTableModel
}