examsmodel.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef EXAMSMODEL_H
  2. #define EXAMSMODEL_H
  3. #include <QAbstractTableModel >
  4. #include <QObject>
  5. class ExamsTableModel : public QAbstractTableModel
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit ExamsTableModel(QObject *parent = nullptr);
  10. struct Exams
  11. {
  12. qint64 id;
  13. QString name;
  14. QString fileDir;
  15. bool select;
  16. int maxTime; // 最大完成时间
  17. };
  18. // 加载 JSON 数据
  19. void loadJsonData(const QJsonArray &jsonArray);
  20. Exams getCurrentExam(const QModelIndex &index) const
  21. {
  22. if (index.isValid() && index.row() < examsList.size()) {
  23. return examsList.at(index.row());
  24. }
  25. return Exams(); // 如果索引无效或者超出范围,返回一个空的 Exams 结构
  26. }
  27. protected:
  28. // 返回行数
  29. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  30. // 返回列数
  31. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  32. // 返回数据
  33. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  34. // 设置表头
  35. QVariant headerData(int section,
  36. Qt::Orientation orientation,
  37. int role = Qt::DisplayRole) const override;
  38. private:
  39. QList<Exams> examsList;
  40. };
  41. #endif // EXAMSMODEL_H