-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentlist.cpp
103 lines (81 loc) · 2.89 KB
/
studentlist.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
95
96
97
98
99
100
101
102
103
#include "studentlist.h"
#include <QDebug>
StudentList::StudentList(QObject* parent):QAbstractListModel(parent)
{
}
StudentList::~StudentList()
{
}
int StudentList::rowCount(const QModelIndex&) const
{
return listOfStudents.size();
}
QVariant StudentList::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= listOfStudents.size())
return QVariant();
{
switch (role) {
case name:
return QVariant(listOfStudents.at(index.row()).getPersonSt());
case number:
return QVariant(listOfStudents.at(index.row()).getNumberBibl());
case count:
return QVariant(listOfStudents.at(index.row()).getCountBooks());
case student_id:
return index.row();
default:
return QVariant();
}
}
}
QHash<int, QByteArray> StudentList::roleNames() const
{
QHash<int, QByteArray> roles;
roles[name] = "NameOfStudent";
roles[number] = "DocumentNumber";
roles[count] = "NumberOfBooks";
roles[student_id] = "Id_student";
return roles;
}
void StudentList::add(const QString& nameSt, const int numberSt, const int countSt){
Student student;
student.setPersonSt(nameSt);
student.setNumberBibl(numberSt);
student.setCountBooks(countSt);
beginInsertRows(QModelIndex(),listOfStudents.size(),listOfStudents.size());
listOfStudents.append(student); //добавление в конец списка
endInsertRows();
}
QAbstractListModel* StudentList::getModel(){
return this;
}
void StudentList::del(const int index){
if (index >= 0 && index < listOfStudents.size())
{
// сообщение модели о процессе удаления данных
beginRemoveRows(QModelIndex(), index, index);
listOfStudents.removeAt(index);
endRemoveRows();
}
else qDebug() << "Error index";
}
void StudentList::edit(const QString& nameSt, const int numberSt, const int countSt, const int index) {
if(index >= 0 && index < listOfStudents.size() )
{
auto& currentStudent = listOfStudents[index];
if (currentStudent.getPersonSt().compare(nameSt)!=0 || currentStudent.getNumberBibl() != numberSt || currentStudent.getCountBooks() != countSt)
{
currentStudent.setPersonSt(nameSt);
currentStudent.setNumberBibl(numberSt);
currentStudent.setCountBooks(countSt);
auto modelIndex = createIndex(index, 0);
emit dataChanged(modelIndex, modelIndex);
qDebug() << listOfStudents[index].getNumberBibl();
}
}
else qDebug() << "Error index";
}
bool StudentList::isConnectionOpen(){
return true;
}