-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.cpp
77 lines (67 loc) · 1.95 KB
/
highscore.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
#include "highscore.h"
#include "ui_highscore.h"
HighScore::HighScore(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::HighScore)
{
ui->setupUi(this);
Load();
}
HighScore::~HighScore()
{
delete ui;
}
void HighScore::Load(){
QList<ScoreUser> userList;
QString fPath = QGuiApplication::applicationDirPath() + "/resourses/files/save.txt";
QFile file(fPath);
if(!file.open(QIODevice::ReadOnly)){
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_5_8);
ScoreUser scoreUser;
while(!in.atEnd()){
in >> scoreUser;
userList.append(scoreUser);
}
file.close();
if(userList.size()>1){
for(int k = 1; k < (userList.size()); k++){
for(int i=0 ;i <(userList.size()-1);i++ ){
if(userList.at(i).myScore < userList.at(i+1).myScore){
userList.swap(i,i+1);
}
}
}
}
int row =10;
if(userList.size()<row){
row =userList.size();
}
ui->tableWidget->setRowCount(row);
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setHorizontalHeaderLabels(list);
ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
for(int i =0;i<row;i++){
QTableWidgetItem* item1 = new QTableWidgetItem();
QTableWidgetItem* item2 = new QTableWidgetItem();
item1->setText(userList.at(i).getName());
item2->setText(QString::number(userList.at(i).getScore()));
ui->tableWidget->setItem(i,0,item1);
ui->tableWidget->setItem(i,1,item2);
}
ui->tableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
}
void HighScore::on_btnExit_clicked()
{
this->close();
}
void HighScore::on_btnPlayAgein_clicked()
{
SnakeWindow *retryGame;
retryGame = new SnakeWindow();
this->hide();
retryGame->show();
this->close();
}