Qt Utilities  5.6.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
checklistmodel.h
Go to the documentation of this file.
1 #ifndef MODELS_CHECKLISTMODEL_H
2 #define MODELS_CHECKLISTMODEL_H
3 
4 #include "../global.h"
5 
6 #include <QAbstractListModel>
7 #include <QList>
8 
9 QT_FORWARD_DECLARE_CLASS(QSettings)
10 
11 namespace Models {
12 
13 class ChecklistModel;
14 
16 {
17  friend class ChecklistModel;
18 
19 public:
20  ChecklistItem(const QVariant &id = QVariant(), const QString &label = QString(), Qt::CheckState checked = Qt::Unchecked);
21 
22  const QVariant &id() const;
23  const QString &label() const;
24  Qt::CheckState checkState() const;
25  bool isChecked() const;
26 
27 private:
28  QVariant m_id;
29  QString m_label;
30  Qt::CheckState m_checkState;
31 };
32 
33 inline ChecklistItem::ChecklistItem(const QVariant &id, const QString &label, Qt::CheckState checkState) :
34  m_id(id),
35  m_label(label),
36  m_checkState(checkState)
37 {}
38 
42 inline const QVariant &ChecklistItem::id() const
43 {
44  return m_id;
45 }
46 
50 inline const QString &ChecklistItem::label() const
51 {
52  return m_label;
53 }
54 
58 inline Qt::CheckState ChecklistItem::checkState() const
59 {
60  return m_checkState;
61 }
62 
67 inline bool ChecklistItem::isChecked() const
68 {
69  return m_checkState == Qt::Checked;
70 }
71 
72 class QT_UTILITIES_EXPORT ChecklistModel : public QAbstractListModel
73 {
74  Q_OBJECT
75 public:
76  explicit ChecklistModel(QObject *parent = nullptr);
77 
78  int rowCount(const QModelIndex &parent = QModelIndex()) const;
79  Qt::ItemFlags flags(const QModelIndex &index) const;
80  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
81  QMap<int, QVariant> itemData(const QModelIndex &index) const;
82  bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
83  bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
84  virtual QString labelForId(const QVariant &id) const;
85  Qt::DropActions supportedDropActions() const;
86  bool insertRows(int row, int count, const QModelIndex &parent);
87  bool removeRows(int row, int count, const QModelIndex &parent);
88  const QList<ChecklistItem> &items() const;
89  void setItems(const QList<ChecklistItem> &items);
90  void restore(QSettings &settings, const QString &name);
91  void save(QSettings &settings, const QString &name) const;
92  static inline constexpr int idRole();
93 
94 private:
95  QList<ChecklistItem> m_items;
96 
97 };
98 
102 inline const QList<ChecklistItem> &ChecklistModel::items() const
103 {
104  return m_items;
105 }
106 
110 inline constexpr int ChecklistModel::idRole()
111 {
112  return Qt::UserRole + 1;
113 }
114 
115 }
116 
117 #endif // MODELS_CHECKLISTMODEL_H
const QVariant & id() const
Returns the ID of the item.
friend class ChecklistModel
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
static constexpr int idRole()
Returns the role used to get or set the item ID.
Qt::CheckState checkState() const
Returns the check state.
The ChecklistItem class provides an item for use with the ChecklistModel class.
The ChecklistModel class provides a generic model for storing checkable items.
const QString & label() const
Returns the label.
bool isChecked() const
Returns whether the item is checked.
Provides common models.
const QList< ChecklistItem > & items() const
Returns the items.