TableView from qml-components for desktop and custom model
-
wrote on 30 Dec 2012, 00:39 last edited by
Hi
I'm still porting my project to Qt5 and new qml-components.
I have a simple custom model and I try to make TableView show data from it, but it never happens, although it's not a problem to show data from ListModel defined in the same qml-file.
Here is my qml
@import QtQuick 2.0
import QtDesktop 1.0Rectangle {
width: 360
height: 360ListModel {
id:listmodel
ListElement{ myrole1: "value 1"; myrole2: "value 2"}
ListElement{ myrole1: "value 3"; myrole2: "value 4"}
}TableView
{
anchors.fill: parentTableColumn{ role: "myrole1" ; title: "Column 1" ; width:100}
TableColumn{ role: "myrole2" ; title: "Column 2" ; width:200}
model:listmodel // *1
//model: mymodel // *2itemDelegate: Text{
height: 30
text: itemValue
}
}
}@It works, but if I comment *1 and uncomment *2 it shows 5 empty rows
Here is the code for mymodel
@#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QtQml/QQmlEngine>
#include <QtWidgets/QApplication>
#include <QAbstractListModel>
#include <QtQml>
#include "MyModel.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);qmlRegisterType<CollectionModel>("CollectionModel", 1, 0, "CollectionModel");
CollectionModel model;
QtQuick2ApplicationViewer viewer;
viewer.rootContext()->setContextProperty("mymodel", &model);
viewer.setMainQmlFile(QStringLiteral("qml/temp/main.qml"));
viewer.showExpanded();return app.exec();
}@
MyModel.h
@#include <QAbstractListModel>
#include <QVariant>class CollectionModel : public QAbstractListModel
{
Q_OBJECTpublic:
enum ERoles {
MyRole1 = Qt::UserRole + 1,
MyRole2
};public:
virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const override
{
return 5;
}virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const override
{
return 2;
}virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const override
{
if(role == MyRole1)
return "role1 value " + QString::number(index.row());
else if(role == MyRole2)
return "role2 value " + QString::number(index.row());return QVariant();
}virtual Qt::ItemFlags flags ( const QModelIndex & index ) const override
{
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}private:
QHash<int, QByteArray> roleNames() const override
{
QHash<int, QByteArray> roles;
roles[MyRole1] = "myrole1";
roles[MyRole2] = "myrole2";
return roles;
}
};@The same code worked in previous version of qml-components
The problem so far is in function getValue of TableView
@function getValue() {
if (header[index].role.length && model.get && model.get(rowIndex)[header[index].role])
return model.get(rowIndex)[header[index].role]
else if (modelData && modelData.hasOwnProperty(header[index].role))
return modelData[header[index].role]
else if (modelData)
return modelData
return ""
}@
It has been changed in the new version and now I get only empty string from it, because model.get is undefined and the same is modelDataCan anybody see what is wrong here or share your working code with custom model
-
wrote on 9 Jan 2013, 13:56 last edited by
This was a regression and should be fixed with https://codereview.qt-project.org/#change,44046
Cheers. -
wrote on 9 Jan 2013, 14:06 last edited by
Great, thanks
It's awesome that everything gets fixed so fast