Reputation: 1284
I've connected source model to my proxy model and my proxy model as a model to a view:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),model_(new Model(this)),proxy_(new Proxy(this))
{
setupUi(this);
proxy_->setSourceModel(model_);
listView->setModel(proxy_);
}
In Proxy class I have a fnc:
int Proxy::rowCount(const QModelIndex&) const
{
static int a = 0;
qDebug() << "Proxy::rowCount sourceModel()->rowCount() " << a++ << ": "<< sourceModel()->rowCount();
return sourceModel()->rowCount();
}
but this is not called when I add something to view via model's fnc:
bool Model::set_data(int data)
{
beginInsertRows(QModelIndex(),0,data_.size());
data_.append(data);
static int a = 0;
qDebug() << "Model::set_data data_ " << a++ << ":" << data_;
endInsertRows();
emit dataChanged(createIndex(0,0),createIndex(data_.size(),0));
return true;
}
The function above is connected via SIGNAL SLOT connection a button on a dialog:
QObject::connect(pushButton, SIGNAL(clicked()), Dialog, SLOT(insert())); and the insert from dialog looks like this:
bool Dialog::insert()
{
static int a = 0;
return model_->set_data(a++);
}
But despite all this view doesn't show anything. On the other hand if I connect as a model to a view my Model class obj instead of Proxy everything works.
Anyone have any idea what's wrong here?
EDIT::
After testing model:
ASSERT failure in QList<T>::at: "index out of range", file c:\QtSDK\Desktop\Qt\4.7.4\mingw\include/QtCore/qlist.h, line 456
After testing just proxy:
D:\...\tst_mpv.exe exited with code -1073741819
My main fnc looks:
#include <QApplication>
#include "Dialog.h"
#include "Model.h"
#include "Proxy.h"
#include "modeltest.h"
int main(int c,char**v)
{
QApplication app(c,v);
/*Model* m = new Model;
new ModelTest(m);*/
Proxy* p = new Proxy;
new ModelTest(p);
/*Dialog d;
d.show();*/
return app.exec();
}
here are my Model and Proxy classes: http://pastebin.com/DiAAkiNY
Upvotes: 1
Views: 476
Reputation: 8157
Here is a complete example of a proxy model, using (as recommended in the documentation) a QSortFilterProxyModel
.
Building up from the QSortFilterProxyModel
is the easiest way as all the tricky bits are done.
test.cpp
#include <QtGui>
#include "proxy.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QStringList list;
list << "ant" << "bear" << "cat" << "dog";
QStringListModel mdl(list);
QListView viewRaw;
viewRaw.setModel(&mdl);
viewRaw.show();
Proxy proxy;
proxy.setSourceModel(&mdl);
QListView viewPrx;
viewPrx.setModel(&proxy);
viewPrx.show();
return app.exec();
}
proxy.h
#ifndef _PROXY_H_
#define _PROXY_H_
#include <QtGui>
class Proxy : public QSortFilterProxyModel
{
public:
virtual QVariant data(const QModelIndex& proxyIndex, int role = Qt::DisplayRole) const;
};
#endif
proxy.cpp
#include "proxy.h"
QVariant Proxy::data(const QModelIndex& proxyIndex, int role) const
{
QVariant d = QSortFilterProxyModel::data(proxyIndex, role);
if (proxyIndex.isValid() && role == Qt::DisplayRole)
return QVariant(QString("[[%1]]").arg(d.toString()));
return d;
}
test.pro
QT += core gui
SOURCES=test.cpp proxy.cpp
HEADERS=proxy.h
Upvotes: 1