PDA

View Full Version : how to use QIdentityProxyModel?



ecanela
3rd August 2012, 02:03
What is the correct way to subclass or use the QIdentityProxyModel?

the next are the minimal example who represent my problem. MyIdentityModel can modify the DisplayRole value (change de capitalitation for upper) but the value in ListView are from the listModel not IdentModel



#include <QtGui>

#include <QDebug>

class MyIdentityModel : public QIdentityProxyModel
{
public:
MyIdentityModel(QObject* parent = 0): QIdentityProxyModel(parent)
{ qDebug() << "constructor"; }

QVariant data(const QModelIndex &index, int role) {
QVariant var = QIdentityProxyModel::data(index,role);
if (role == Qt::DisplayRole)
return var.toString().toUpper();

return var;
}
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStringList list;
list << "uno" << "dos" << "tres" << "cuatro"
<< "cinco" << "seis" << "siete";

QStringListModel *listModel = new QStringListModel;
listModel->setStringList(list);

MyIdentityModel * identModel = new MyIdentityModel;
identModel->setSourceModel(listModel); //setSource model works?

//The ident proxy works fine. convert Display role to Upper
//but value display in QListView are from listModel
qDebug() << "data" << identModel->data(
identModel->index(3,0),
Qt::DisplayRole);

QListView *w = new QListView;
w->setModel(identModel);
w->show();

return a.exec();
}

ChrisW67
3rd August 2012, 02:46
The prototype of your data() function does not match the prototype of the data() function you are trying to override: you are missing a const qualifier.

ecanela
3rd August 2012, 05:11
almost four hours of trial and error and the error was a missing type in the doc of QIdentityProxyModel... but, I feel like a fool for not having noticed before.

let me report the missing typo in the doc to trolltech.


thanks for the help.

ChrisW67
3rd August 2012, 07:10
I see no error in the documentation of the data() function in QAbstractProxyModel, from which it is inherited by QIdentityProxyModel. There is an error in the snippet in the QIdentityProxyModel detailed description section. Attach a patch to your bug.


diff -u a/doc/src/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp b/doc/src/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp
--- a/doc/src/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp 2012-03-15 00:01:52.000000000 +1000
+++ b/doc/src/snippets/code/src_gui_itemviews_qidentityproxymodel.cpp 2012-08-03 16:03:09.000000000 +1000
@@ -48,7 +48,7 @@
m_formatString = formatString;
}

- QVariant data(const QModelIndex &index, int role)
+ QVariant data(const QModelIndex &index, int role) const
{
if (role != Qt::DisplayRole)
return QIdentityProxyModel::data(index, role);


BTW: Trolltech have not existed for quite a while.

ecanela
3rd August 2012, 20:29
patch submitted. thanks for the diff file.