Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved DelegateChooser not working with TableView and QAbstractTableModel

    QML and Qt Quick
    delegatechooser tableview
    1
    1
    386
    Loading More Posts
    • 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
      notsak last edited by

      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 Reply Quote 0
      • First post
        Last post