Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ListItem: public QObject {
- Q_OBJECT
- public:
- enum Roles {
- IdRole = Qt::UserRole+1,
- NameRole,
- NumQuestionsRole,
- NumQuestionsNoCasesRole
- };
- ListItem(QObject* parent = 0) : QObject(parent) {}
- explicit ListItem(const int &id, const QString &name, const int &numQuestions, const int &numQuestionNoCases, QObject *parent = 0);
- ~ListItem() {}
- QVariant data(int role) const;
- QHash<int, QByteArray> roleNames() const;
- inline int id() const { return m_id; }
- inline QString name() const { return m_name; }
- inline int num_q() const { return m_numQuestions; }
- inline int num_qnc() const { return m_numQuestionsNoCases; }
- signals:
- void dataChanged();
- private:
- int m_id;
- QString m_name;
- int m_numQuestions;
- int m_numQuestionsNoCases;
- };
- class QmlAbstractListModel : public QAbstractListModel
- {
- Q_OBJECT
- public:
- // explicit QmlAbstractListModel(ListItem* prototype, QObject* parent = 0);
- explicit QmlAbstractListModel(QObject* parent = 0);
- ~QmlAbstractListModel();
- int rowCount(const QModelIndex &parent = QModelIndex()) const;
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- virtual void appendRow(ListItem* item);
- virtual void appendRows(const QList<ListItem*> &items);
- void insertRow(int row, ListItem* item);
- bool removeRow(int row, const QModelIndex &parent = QModelIndex());
- bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
- ListItem* takeRow(int row);
- ListItem* find(const int &id) const;
- QModelIndex indexFromItem( const ListItem* item) const;
- void clear();
- QList<ListItem*> m_list;
- private:
- // ListItem* m_prototype;
- private slots:
- void handleItemChange();
- };
- class QmlSubjectListModel : public QmlAbstractListModel
- {
- Q_OBJECT
- public:
- explicit QmlSubjectListModel(QObject *parent = 0);
- private:
- void readSqlData();
- void reload();
- void appendRow(ListItem *item);
- void appendRows(const QList<ListItem *> &items);
- };
- void QmlSubjectListModel::readSqlData()
- {
- DEBUG_BLOCK
- QSqlDatabase db=QSqlDatabase::database(QUESTIONSDB);
- if (db.isOpen())
- {
- DBQuery q(db);
- q.setForwardOnly(true);
- q.exec(QString("SELECT s.id, s.name, COUNT(q.subjectId), "
- "COUNT(NULLIF(q.caseId, 0)) FROM "
- "tSubjects AS s, tQuestions AS q WHERE "
- "s.name <> 'Fallbeschreibung' AND "
- "s.name <> 'root' AND s.id = q.subjectId "
- "GROUP BY q.subjectId ORDER BY s.name ASC"));
- while(q.next()) {
- ListItem *item=new ListItem( q.value(0).toInt(), //id
- q.value(1).toString(), //name
- q.value(2).toInt(), //number of questions
- q.value(2).toInt()-q.value(3).toInt()); //number of questions excluding cases
- appendRow(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement