Created
July 29, 2011 08:37
-
-
Save dubik/1113448 to your computer and use it in GitHub Desktop.
QSortFilterProblem
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
#include <QDebug> | |
#include "recentlyopenedmodel.h" | |
RecentlyOpenedModel::RecentlyOpenedModel(QObject *parent) : | |
QSortFilterProxyModel(parent), maxItemsCount(INT_MAX) | |
{ | |
setDynamicSortFilter(true); | |
} | |
int RecentlyOpenedModel::maxItems() const | |
{ | |
return maxItemsCount; | |
} | |
void RecentlyOpenedModel::setMaxItems(int count) | |
{ | |
if(maxItemsCount != count) { | |
maxItemsCount = count; | |
emit maxItemsChanged(); | |
invalidateFilter(); | |
} | |
} | |
bool RecentlyOpenedModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const | |
{ | |
Q_UNUSED(source_parent); | |
qDebug() << "filterAcceptsRow " << source_row; | |
return source_row < maxItems(); | |
} |
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
#ifndef RECENTLYOPENEDMODEL_H | |
#define RECENTLYOPENEDMODEL_H | |
#include <QObject> | |
#include <QSortFilterProxyModel> | |
class RecentlyOpenedModel : public QSortFilterProxyModel | |
{ | |
Q_OBJECT | |
public: | |
explicit RecentlyOpenedModel(QObject *parent = 0); | |
Q_PROPERTY(int maxItems READ maxItems WRITE setMaxItems NOTIFY maxItemsChanged) | |
int maxItems() const; | |
protected: | |
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; | |
signals: | |
void maxItemsChanged(); | |
public slots: | |
void setMaxItems(int count); | |
private: | |
int maxItemsCount; | |
}; | |
#endif // RECENTLYOPENEDMODEL_H |
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
#include <QTest> | |
#include <QHash> | |
#include <QDebug> | |
#include "recentlyopenedmodel.h" | |
#include "bookstoremodel.h" | |
#include "tst_modelstest.h" | |
TstModelsTest::~TstModelsTest() | |
{ | |
} | |
void TstModelsTest::initTestCase() | |
{ | |
store = new TestBookStore; | |
addBook("B1", QDateTime::fromString("10", "d")); | |
addBook("B2", QDateTime::fromString("11", "d")); | |
addBook("B3", QDateTime::fromString("9", "d")); | |
addBook("B4", QDateTime::fromString("6", "d")); | |
addBook("B5", QDateTime::fromString("13", "d")); | |
addBook("B6", QDateTime::fromString("14", "d")); | |
storeModel = new BookStoreModel(store); | |
} | |
void TstModelsTest::cleanupTestCase() | |
{ | |
delete storeModel; | |
qDeleteAll(store->books); | |
delete store; | |
} | |
void TstModelsTest::init() | |
{ | |
model = new RecentlyOpenedModel; | |
model->setSourceModel(storeModel); | |
} | |
void TstModelsTest::cleanup() | |
{ | |
delete model; | |
} | |
void TstModelsTest::addBook(const QString& title, const QDateTime& dateTime) | |
{ | |
Book * book = new Book; | |
book->setTitle(title); | |
book->setCreated(dateTime); | |
store->books.append(book); | |
} | |
void TstModelsTest::testDefaultMaxItem() | |
{ | |
QCOMPARE(model->rowCount(), 6); | |
} | |
void TstModelsTest::testMaxItemIsTwo() | |
{ | |
model->setMaxItems(2); | |
QCOMPARE(model->rowCount(), 2); | |
} | |
void TstModelsTest::testSortingByRole() | |
{ | |
model->setSortRole(BookStoreModel::CreatedRole); | |
model->sort(0, Qt::DescendingOrder); | |
QCOMPARE(model->data(model->index(0, 0), BookStoreModel::TitleRole).toString(), QString("B6")); | |
QCOMPARE(model->data(model->index(1, 0), BookStoreModel::TitleRole).toString(), QString("B5")); | |
QCOMPARE(model->data(model->index(2, 0), BookStoreModel::TitleRole).toString(), QString("B2")); | |
QCOMPARE(model->data(model->index(3, 0), BookStoreModel::TitleRole).toString(), QString("B1")); | |
QCOMPARE(model->data(model->index(4, 0), BookStoreModel::TitleRole).toString(), QString("B3")); | |
QCOMPARE(model->data(model->index(5, 0), BookStoreModel::TitleRole).toString(), QString("B4")); | |
} | |
void TstModelsTest::testSortAndFilter() | |
{ | |
model->setSortRole(BookStoreModel::CreatedRole); | |
model->setMaxItems(2); | |
model->sort(0, Qt::DescendingOrder); | |
QCOMPARE(model->rowCount(), 2); | |
QCOMPARE(model->data(model->index(0, 0), BookStoreModel::TitleRole).toString(), QString("B6")); | |
QCOMPARE(model->data(model->index(1, 0), BookStoreModel::TitleRole).toString(), QString("B5")); | |
} | |
int main(int argc, char ** argv) | |
{ | |
TstModelsTest testObject; | |
return QTest::qExec(&testObject, argc, argv); | |
} |
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
#ifndef TST_MODELSTEST_H__ | |
#define TST_MODELSTEST_H__ | |
#include <QObject> | |
#include <QVector> | |
#include "book.h" | |
#include "bookstoreinterface.h" | |
class BookStoreModel; | |
class RecentlyOpenedModel; | |
class TestBookStore : public BookStoreInterface | |
{ | |
public: | |
virtual ~TestBookStore() {} | |
virtual Book * bookAt(int index) const { | |
return books[index]; | |
} | |
virtual int bookCount() const { | |
return books.size(); | |
} | |
QVector<Book *> books; | |
}; | |
class TstModelsTest : public QObject | |
{ | |
Q_OBJECT | |
public: | |
TstModelsTest() : QObject() { | |
} | |
virtual ~TstModelsTest(); | |
private slots: | |
void initTestCase(); | |
void cleanupTestCase(); | |
void init(); | |
void cleanup(); | |
void testDefaultMaxItem(); | |
void testMaxItemIsTwo(); | |
void testSortingByRole(); | |
void testSortAndFilter(); | |
private: | |
void addBook(const QString& title, const QDateTime& dateTime); | |
private: | |
TestBookStore * store; | |
BookStoreModel * storeModel; | |
RecentlyOpenedModel * model; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment