daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Oct 11th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ScoreRecord : public QObject
  2. {
  3.     Q_OBJECT
  4.  
  5.     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
  6.     Q_PROPERTY(QString date READ date WRITE setDate NOTIFY dateChanged)
  7.     Q_PROPERTY(QString score READ score WRITE setScore NOTIFY scoreChanged)
  8. public:
  9.     ScoreRecord(QObject *parent = 0);
  10.     ScoreRecord(const QString& n, const QString &d, const QString &s, QObject *parent = 0);
  11.     QString name() const;
  12.     void setName(const QString &str);
  13.     QString date() const;
  14.     void setDate(const QString &str);
  15.     QString score() const;
  16.     void setScore(const QString &str);
  17.  
  18.  
  19. signals:
  20.     void nameChanged();
  21.     void dateChanged();
  22.     void scoreChanged();
  23.  
  24. public slots:
  25.  
  26. private:
  27.     QString m_name;
  28.     QString m_date;
  29.     QString m_score;
  30. };
  31.    
  32. class ScoreHandler : public QObject
  33. {
  34.     Q_OBJECT
  35. private:
  36.     const char* SCORE_TABLE_FILENAME;
  37.  
  38.     struct scoreRow {
  39.         char name[128];
  40.         char date[32];
  41.         char score[16];
  42.     };
  43.  
  44.  
  45. public:
  46.     explicit ScoreHandler(QObject *parent = 0);
  47.     QList<ScoreRecord *> scoreList;
  48.  
  49. signals:
  50.  
  51. public slots:
  52.     void SaveScore(const QString &name, const QString &date, const QString &score);
  53.     void LoadScore();
  54. };
  55.    
  56. int main(int argc, char *argv[])
  57. {
  58.  
  59.     QGuiApplication app(argc, argv);
  60.     QtQuick2ApplicationViewer viewer;
  61.  
  62.     ScoreHandler* scoreHandler = new ScoreHandler();
  63.     QQmlContext* ctx = viewer.rootContext();
  64.     ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreHandler->scoreList));
  65.     viewer.setMainQmlFile(QStringLiteral("qml/qmlListView/main.qml"));
  66.  
  67.     viewer.showExpanded();
  68.  
  69.     return app.exec();
  70. }
  71.    
  72. import QtQuick 2.0
  73.  
  74. Rectangle {
  75.     width: 360
  76.     height: 360
  77.  
  78.     ListView {
  79.         width: 100; height: 100
  80.         anchors.fill: parent
  81.  
  82.         model: MyScoreModel
  83.         delegate: Text {
  84.             text: name
  85.         }
  86.     }
  87. }
  88.    
  89. int main(int argc, char *argv[])
  90. {
  91.  
  92.     QGuiApplication app(argc, argv);
  93.     QtQuick2ApplicationViewer viewer;
  94.  
  95.     ScoreHandler* scoreHandler = new ScoreHandler();
  96.     QList<QObject *> scoreList;
  97.  
  98.     scoreList.append(new ScoreRecord("Jmeno1", "datum1", "score1"));
  99.     scoreList.append(new ScoreRecord("Jmeno2", "datum2", "score2"));
  100.     scoreList.append(new ScoreRecord("Jmeno3", "datum3", "score3"));
  101.  
  102.     QQmlContext* ctx = viewer.rootContext();
  103.     ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreList));
  104.     viewer.setMainQmlFile(QStringLiteral("qml/qmlListView/main.qml"));
  105.  
  106.     viewer.showExpanded();
  107.  
  108.     return app.exec();
  109. }
  110.    
  111. Q_PROPERTY(QQmlListProperty<ScoreRecord> scoreList READ scoreList)
  112.    
  113. // ScoreHandler.h
  114. QQmlListProperty<ScoreRecord> scoreList();
  115.  
  116. // ScoreHandler.cpp
  117. QQmlListProperty<ScoreRecord> ScoreHandler::scoreList()
  118. {
  119.     return QQmlListProperty<ScoreRecord>(this, _scoreRecords);
  120. }
  121.    
  122. ctx->setContextProperty("MyScoreModel", QVariant::fromValue(scoreHandler->scoreList));
  123.    
  124. [{name: Jmeno1, date: datum1, score: score1},
  125. {name: Jmeno2, date: datum3, score: score2},
  126. {name: Jmeno3, date: datum3, score: score3}]
  127.    
  128. Q_PROPERTY(QQmlListProperty<myNamespace::ScoreRecord> scoreList READ scoreList)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top