TableView from qml-components for desktop and custom model



  • 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.0

    Rectangle {
    width: 360
    height: 360

    ListModel {
    id:listmodel
    ListElement{ myrole1: "value 1"; myrole2: "value 2"}
    ListElement{ myrole1: "value 3"; myrole2: "value 4"}
    }

    TableView
    {
    anchors.fill: parent

    TableColumn{ role: "myrole1" ; title: "Column 1" ; width:100}
    TableColumn{ role: "myrole2" ; title: "Column 2" ; width:200}
    model:listmodel // *1
    //model: mymodel // *2

    itemDelegate: 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_OBJECT

    public:
    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 modelData

    Can anybody see what is wrong here or share your working code with custom model



  • This was a regression and should be fixed with https://codereview.qt-project.org/#change,44046
    Cheers.



  • Great, thanks
    It's awesome that everything gets fixed so fast


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.