Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. TableView from qml-components for desktop and custom model
Forum Update on Monday, May 27th 2025

TableView from qml-components for desktop and custom model

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ittennull
    wrote on 30 Dec 2012, 00:39 last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jens
      wrote on 9 Jan 2013, 13:56 last edited by
      #2

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ittennull
        wrote on 9 Jan 2013, 14:06 last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved