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. DelegateChooser not working with TableView and QAbstractTableModel

DelegateChooser not working with TableView and QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
delegatechoosertableview
1 Posts 1 Posters 605 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.
  • N Offline
    N Offline
    notsak
    wrote on last edited by
    #1

    I am trying to implement a very simple 1-column TableView using a DelegateChooser. I get the following errors when I run:

    TableView: failed loading index: 0
    TableView: failed loading index: 1
    TableView: failed loading index: 2
    

    Any ideas?

    main.qml:

    import QtQuick 2.12
    import QtQuick.Controls 2.4
    import Qt.labs.qmlmodels 1.0
    
    import qt.abc.testmodel 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
    
        TestModel {
            id: testModel
        }
    
        TableView {
            id: testTable
            anchors.fill: parent
            model: testModel
    
            delegate: DelegateChooser {
                role: "type"
    
                DelegateChoice {
                    roleValue: "name"
                    Text {
                        text: model.display
                    }
                }
            } /* DelegateChooser */
        } /* TableView */
    }
    
    

    testmodel.h:

    #ifndef TESTMODEL_H
    #define TESTMODEL_H
    
    #include <QAbstractTableModel>
    
    class TestModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        enum Role
        {
            Type = Qt::UserRole
        };
        Q_ENUM(Role)
    
        explicit TestModel(QObject* parent = nullptr);
        int rowCount(const QModelIndex& parent = QModelIndex()) const override;
        int columnCount(const QModelIndex& parent = QModelIndex()) const override;
        QVariant data(const QModelIndex& idx, int role) const override;
        QHash<int, QByteArray> roleNames() const override;
    
    signals:
    
    public slots:
    
    private:
        QVector<QString> mWords;
        QHash<int, QByteArray> mRoleNames;
    };
    
    #endif // TESTMODEL_H
    
    

    testmodel.cpp:

    #include "testmodel.h"
    
    #include <QDebug>
    #include <QHash>
    
    TestModel::TestModel(QObject* parent) : QAbstractTableModel(parent)
    {
        mRoleNames = QAbstractTableModel::roleNames();
        mRoleNames.insert(Role::Type, QByteArray("type"));
    
        mWords << "blah"
               << "foo"
               << "beep";
    }
    
    int TestModel::rowCount(const QModelIndex& parent) const
    {
        Q_UNUSED(parent);
        return mWords.size();
    }
    
    int TestModel::columnCount(const QModelIndex& parent) const
    {
        Q_UNUSED(parent);
        return 1;
    }
    
    QVariant TestModel::data(const QModelIndex& idx, int role) const
    {
        // return empty QVariant for invalid index
        if (!idx.isValid() || idx.row() >= mWords.size() || idx.row() < 0) {
            return QVariant();
        }
    
        switch (role) {
        case Qt::DisplayRole:
            return mWords[idx.row()];
        case Role::Type:
            return "name";
        default:
            return QVariant(); // should never get here
        }
    }
    
    QHash<int, QByteArray> TestModel::roleNames() const
    {
        return mRoleNames;
    }
    
    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