Add a clear() method to empty the list of the model (better than
creating a new one every time) and rename m_posts to m_items
This commit is contained in:
parent
6a3a866ad5
commit
521bdad42c
1 changed files with 30 additions and 14 deletions
|
@ -22,7 +22,7 @@
|
|||
|
||||
// Own
|
||||
|
||||
#include "danboorutablemodel.h"
|
||||
#include "danboorupostmodel.h"
|
||||
#include "libdanbooru/danboorupost.h"
|
||||
|
||||
// Qt
|
||||
|
@ -32,47 +32,50 @@
|
|||
|
||||
namespace Danbooru {
|
||||
|
||||
DanbooruTableModel::DanbooruTableModel(QObject* parent): QAbstractListModel(parent)
|
||||
DanbooruPostModel::DanbooruPostModel(QObject* parent): QAbstractListModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DanbooruTableModel::~DanbooruTableModel()
|
||||
DanbooruPostModel::~DanbooruPostModel()
|
||||
{
|
||||
qDeleteAll(m_posts);
|
||||
m_posts.clear();
|
||||
|
||||
if (!m_items.isEmpty()) {
|
||||
qDeleteAll(m_items);
|
||||
m_items.clear();
|
||||
}
|
||||
}
|
||||
|
||||
int DanbooruTableModel::rowCount(const QModelIndex& parent) const
|
||||
int DanbooruPostModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
return m_posts.size();
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
void DanbooruTableModel::addPost(Danbooru::DanbooruPost* post)
|
||||
void DanbooruPostModel::addPost(Danbooru::DanbooruPost* post)
|
||||
{
|
||||
|
||||
beginInsertRows(QModelIndex(), m_posts.size(), m_posts.size() + 1);
|
||||
m_posts.append(post);
|
||||
beginInsertRows(QModelIndex(), m_items.size(), m_items.size() + 1);
|
||||
m_items.append(post);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
QVariant DanbooruTableModel::data(const QModelIndex& index, int role) const
|
||||
QVariant DanbooruPostModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (index.row() >= m_posts.size() || index.row() < 0) {
|
||||
if (index.row() >= m_items.size() || index.row() < 0) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (m_posts.isEmpty()) {
|
||||
if (m_items.isEmpty()) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
DanbooruPost* post = m_posts.at(index.row());
|
||||
DanbooruPost* post = m_items.at(index.row());
|
||||
|
||||
if (!post) {
|
||||
return QVariant();
|
||||
|
@ -98,4 +101,17 @@ namespace Danbooru {
|
|||
return QVariant();
|
||||
}
|
||||
|
||||
void DanbooruPostModel::clear() {
|
||||
|
||||
if (m_items.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginRemoveRows(QModelIndex(),0, m_items.size());
|
||||
qDeleteAll(m_items);
|
||||
m_items.clear();
|
||||
endRemoveRows();
|
||||
reset();
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue