-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableModel.cpp
66 lines (53 loc) · 1.28 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
#include "TableModel.h"
#include <algorithm>
TableModelItem::TableModelItem(QString text) : m_text(text)
{
}
QString TableModelItem::text() const
{
return m_text;
}
TableModel::TableModel(QObject* parent) : QAbstractTableModel(parent)
{
}
int TableModel::rowCount(const QModelIndex& /*parent*/) const
{
return m_items.size();
}
int TableModel::columnCount(const QModelIndex& /*parent*/) const
{
return 1;
}
QVariant TableModel::data(const QModelIndex& index, int role) const
{
if (role == Qt::DisplayRole)
{
return m_items[index.row()].text();
}
return QVariant();
}
void TableModel::appendItem(TableModelItem item)
{
const int row = m_items.size();
beginInsertRows(QModelIndex(), row, row);
m_items.push_back(item);
endInsertRows();
}
void TableModel::sortByColumn(int /*column*/, Qt::SortOrder order)
{
if (order == Qt::AscendingOrder)
{
std::sort(m_items.begin( ), m_items.end(),[](const TableModelItem& lhs, const TableModelItem& rhs)
{
return lhs.text() < rhs.text();
});
}
else
{
std::sort(m_items.begin( ), m_items.end(),[](const TableModelItem& lhs, const TableModelItem& rhs)
{
return lhs.text() > rhs.text();
});
}
emit layoutChanged();
}