-
-
Save thanhtam92/1a729e98a9b784cfd50d02654a9a1273 to your computer and use it in GitHub Desktop.
Simple string list inherited from QAbstractListModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SimpleStringList.h | |
#ifndef _SIMPLEMODEL_H_ | |
#define _SIMPLEMODEL_H_ | |
#include <QtCore> | |
class SimpleStringModel : public QAbstractListModel | |
{ | |
Q_PROPERTY(int count READ count NOTIFY countChanged) | |
Q_DISABLE_COPY(SimpleStringModel) | |
Q_OBJECT | |
public: | |
explicit SimpleStringModel(QObject* parent = 0); | |
SimpleStringModel(const QList<QString>& initList, QObject* parent = 0); | |
virtual ~SimpleStringModel(); | |
private: | |
enum Roles{ | |
ModelDataRole = Qt::UserRole+1 | |
}; | |
public: | |
int count() const; | |
public: | |
void append(const QString& item); | |
void insert(int index, const QString& item); | |
void append(const QList<QString>& items); | |
void insert(int index, const QList<QString>& items); | |
bool removeOne(const QString& item); | |
int removeAll(const QString& item); | |
void removeAt(int index); | |
QList<QString> list() const; | |
signals: | |
void countChanged(); | |
// QAbstractItemModel interface | |
public: | |
virtual int rowCount(const QModelIndex &parent) const; | |
virtual QVariant data(const QModelIndex &index, int role) const; | |
virtual QHash<int, QByteArray> roleNames() const; | |
private: | |
QList<QString> m_data; | |
}; | |
#endif //_SIMPLEMODEL_H_ | |
/**************************************************************/ | |
// SimpleStringList.cpp | |
#include <SimpleStringModel.h> | |
SimpleStringModel::SimpleStringModel(QObject *parent) : | |
QAbstractListModel(parent) | |
{ | |
} | |
SimpleStringModel::SimpleStringModel(const QList<QString> &initList, QObject *parent) : | |
QAbstractListModel(parent) | |
{ | |
m_data.append(initList); | |
} | |
SimpleStringModel::~SimpleStringModel() | |
{} | |
int SimpleStringModel::count() const | |
{ | |
return m_data.count(); | |
} | |
void SimpleStringModel::append(const QString &item) | |
{ | |
beginInsertRows(QModelIndex(), m_data.count(), m_data.count()); | |
m_data.append(item); | |
endInsertRows(); | |
emit countChanged(); | |
} | |
void SimpleStringModel::append(const QList<QString> &items) | |
{ | |
if(items.count() == 0) return; | |
beginInsertRows(QModelIndex(), m_data.count(), m_data.count()+items.count()-1); | |
m_data.append(items); | |
endInsertRows(); | |
emit countChanged(); | |
} | |
void SimpleStringModel::insert(int index, const QString &item) | |
{ | |
beginInsertRows(QModelIndex(), index, index+1); | |
m_data.insert(index, item); | |
endInsertRows(); | |
emit countChanged(); | |
} | |
void SimpleStringModel::insert(int index, const QList<QString> &items) | |
{ | |
if(items.count() == 0) return; | |
index = qMax(0, index); | |
Q_ASSERT(index >= m_data.count()); | |
beginInsertRows(QModelIndex(), index, index+items.count()); | |
for(int i = 0, k = index; i < items.count(); ++i, ++k) | |
m_data.insert(k, items.at(i)); | |
endInsertRows(); | |
emit countChanged(); | |
} | |
bool SimpleStringModel::removeOne(const QString &item) | |
{ | |
int row = m_data.indexOf(item); | |
if(row < 0) return false; | |
beginRemoveRows(QModelIndex(), row, row); | |
m_data.removeAt(row); | |
endRemoveRows(); | |
emit countChanged(); | |
return true; | |
} | |
int SimpleStringModel::removeAll(const QString &item) | |
{ | |
if(!m_data.contains(item)) return 0; | |
beginResetModel(); | |
int resultado = m_data.removeAll(item); | |
endResetModel(); | |
emit countChanged(); | |
return resultado; | |
} | |
void SimpleStringModel::removeAt(int index) | |
{ | |
if(index < 0 || index >= count()) return; | |
beginRemoveRows(QModelIndex(), index, index); | |
m_data.removeAt(index); | |
endRemoveRows(); | |
emit countChanged(); | |
} | |
QList<QString> SimpleStringModel::list() const | |
{ | |
return QList<QString>(m_data); | |
} | |
int SimpleStringModel::rowCount(const QModelIndex &parent) const | |
{ | |
Q_UNUSED(parent); | |
return count(); | |
} | |
QVariant SimpleStringModel::data(const QModelIndex &index, int role) const | |
{ | |
if(index.isValid() == false) return QVariant(); | |
if(index.row() < 0 || index.row() >= count()) return QVariant(); | |
switch (role) { | |
case Qt::DisplayRole: | |
case ModelDataRole: | |
return m_data.at(index.row()); | |
default: | |
return QVariant(); | |
} | |
} | |
QHash<int, QByteArray> SimpleStringModel::roleNames() const | |
{ | |
QHash<int, QByteArray> roles; | |
roles[ModelDataRole] = "modelData"; | |
return roles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment