-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductListModel.cpp
More file actions
51 lines (43 loc) · 1.38 KB
/
Copy pathProductListModel.cpp
File metadata and controls
51 lines (43 loc) · 1.38 KB
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
#include "ProductListModel.h"
#include <QString>
ProductListModel::ProductListModel(const QVector<FinancialProduct*>& products, QObject* parent)
: QAbstractTableModel(parent), m_products(products)
{
}
int ProductListModel::rowCount(const QModelIndex&) const
{
return m_products.size();
}
int ProductListModel::columnCount(const QModelIndex&) const
{
return 2;
}
QVariant ProductListModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
FinancialProduct* product = m_products[index.row()];
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) {
case 0: return product->name();
case 1: return QString::number(product->price(), 'f', 2);
}
}
return QVariant();
}
QVariant ProductListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
static const QStringList headers = {"Product Name", "Current Price"};
return section < headers.size() ? headers[section] : QVariant();
}
return QVariant();
}
Qt::ItemFlags ProductListModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
if (index.isValid()) {
flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
return flags;
}