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. ComboBox and QAbstractListModel
Forum Updated to NodeBB v4.3 + New Features

ComboBox and QAbstractListModel

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 5.6k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dmitry89
    wrote on last edited by
    #1

    How can I use QAbstractListModel as a model for ComboBox? It works fine with ListView, but when I set it as a model for ComboBox, my application generates the following warning at startup:
    ComboBox.qml:510: ReferenceError: modelData is not defined

    "textRole" also does not work

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Can you post the relevant code ? It is tougher to guess the problem without it.

      157

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dmitry89
        wrote on last edited by p3c0
        #3
        ComboBox {
                    model: ShipsModel
                    textRole: name
        }
        

        Model:

        ShipsModel::ShipsModel(QObject *parent)
             : QAbstractListModel(parent)
         {
         }
         
         int ShipsModel::rowCount(const QModelIndex & parent) const {
             Q_UNUSED(parent);
             return m_data.count();
         }
         
         QVariant ShipsModel::data(const QModelIndex & index, int role) const {
             if (index.row() < 0 || index.row() >= m_data.count())
                 return QVariant();
         
             const ShipsItem &item = m_data[index.row()];
             if (role == NameRole)
                 return item.name();
             else if (role == SizeRole)
                 return item.size();
             return QVariant();
         }
         
         bool ShipsModel::setData(const QModelIndex &index, const QVariant &value, int role)
         {
             ShipsItem *item = &m_data[index.row()];
         
             if (index.isValid() && role == Qt::EditRole) {
                 if(index.column()==0){
                     item->setName(value.toString());
                     emit dataChanged(index, index);
                 }
                 if(index.column()==1){
                     item->setSize(value.toInt());
                     emit dataChanged(index, index);
                 }
                 return true;
             }
             return false;
         }
         
         QHash<int, QByteArray> ShipsModel::roleNames() const {
             QHash<int, QByteArray> roles;
             roles[NameRole] = "name";
             roles[SizeRole] = "size";
             return roles;
         } 
        

        This code works well:

        ListView {
              model: ShipsModel
              delegate: TextField {
                        text: name
               }
        }
        
        1 Reply Last reply
        1
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          textRole expects a string so it should be "name"

          ReferenceError: modelData is not defined would occur when the Combobox is created (and thus model is set) before the model is actually initialized.

          157

          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