-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.cpp
55 lines (52 loc) · 1.38 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
#include "highscore.h"
#include "ui_highscore.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
/**
* Constructor
*
* @param parent
*/
Highscore::Highscore(QWidget *parent) :
QDialog(parent),
ui(new Ui::Highscore)
{
ui->setupUi(this);
this->load();
}
/**
* Destructor
* Removes all database connections
*/
Highscore::~Highscore()
{
QStringList list = QSqlDatabase::connectionNames();
for(int i = 0; i < list.count(); ++i) {
QSqlDatabase::removeDatabase(list[i]);
}
delete ui;
}
/**
* Loads the highscore from database
*
*/
void Highscore::load()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Highscore");
db.setDatabaseName("./highscore.db");
if (db.open()) {
QSqlQueryModel * model = new QSqlQueryModel();
QSqlQuery query(db);
query.exec("SELECT * FROM score ORDER BY ABS(score1-score2) DESC");
model->setQuery(query);
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Player 1"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Player 2"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr(""));
model->setHeaderData(4, Qt::Horizontal, QObject::tr(""));
this->ui->tableView->setModel(model);
this->ui->tableView->hideColumn(0);
}
db.close();
}