Guest User

Untitled

a guest
Jul 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #ifndef LISTMODEL_H
  2. #define LISTMODEL_H
  3.  
  4. #include <QAbstractListModel>
  5. #include <QList>
  6. #include <QVariant>
  7. //------------------------------------------------------------------------------
  8. class ListItem: public QObject {
  9.   Q_OBJECT
  10.  
  11. public:
  12.   ListItem(QObject* parent = 0) : QObject(parent) {}
  13.   virtual ~ListItem() {}
  14.   virtual QString id() const = 0;
  15.   virtual QVariant data(int role) const = 0;
  16.   virtual QHash<int, QByteArray> roleNames() const = 0;
  17.  
  18. signals:
  19.   void dataChanged();
  20. };
  21. //------------------------------------------------------------------------------
  22. class ListModel : public QAbstractListModel
  23. {
  24.   Q_OBJECT
  25.  
  26. public:
  27.   explicit ListModel(ListItem* prototype, QObject* parent = 0);
  28.   ~ListModel();
  29.   int rowCount(const QModelIndex &parent = QModelIndex()) const;
  30.   void appendRow(ListItem* item);
  31.   void appendRows(const QList<ListItem*> &items);
  32.   void insertRow(int row, ListItem* item);
  33.   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
  34.   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  35.   void clear();
  36.  
  37.   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  38.   ListItem* takeRow(int row);
  39.   ListItem* find(const QString &id) const;
  40.   QModelIndex indexFromItem( const ListItem* item) const;
  41.   QList<ListItem*> getData(){return m_list;}
  42.  
  43. private slots:
  44.   void handleItemChange();
  45. private:
  46.   ListItem* m_prototype;
  47.   QList<ListItem*> m_list;
  48. };
  49. //------------------------------------------------------------------------------
  50. #endif // LISTMODEL_H
Add Comment
Please, Sign In to add comment