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. QStringListModel doesn't show on QML ListView
Forum Updated to NodeBB v4.3 + New Features

QStringListModel doesn't show on QML ListView

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 1 Posters 4.1k 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.
  • S Offline
    S Offline
    swagat
    wrote on last edited by
    #1

    Hi,
    I'm pretty new to QML. I have definded QStringListModel in CPP and exposed to QML. My qml is not able to show list of strings that my model is set to. Please find my code as follows:

    @int main(int argc, char ** argv)
    {
    QGuiApplication app(argc, argv);

    QQuickView view;
    QQmlContext *ctxt = view.rootContext();
    
    QStringListModel *model = new QStringListModel();
    QStringList list;
    list << "a" << "b" << "c"<<"s"<<"w";
    model->setStringList(list);
    
    
    ctxt->setContextProperty("myModel", model);
    

    //![0]

    view.setSource(QUrl("qrc:view.qml"));
    view.show();
    return app.exec();
    

    }
    @

    @import QtQuick 2.0
    //![0]

    ListView {
    width: 100; height: 100

    model: myModel
    delegate: Rectangle {
        height: 25
        width: 100
        Text { text: modelData }
    
    }
    

    }
    @

    Am missing something? Please suggest!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      swagat
      wrote on last edited by
      #2

      can anyone respond. i'm really stuck on this!!!

      1 Reply Last reply
      0
      • S Offline
        S Offline
        swagat
        wrote on last edited by
        #3

        I solved the problem by using role name as Qt::display in my qml file.
        @ import QtQuick 2.0
        //![0]

        ListView {
            width: 100; height: 100
         
            model: myModel
            delegate:
        

        Item{
        Rectangle {
        height: 25
        width: 100
        Text { text: display }

            }
          }
        }
        

        @
        QVariant QStringListModel::data(const QModelIndex &index, int role) implemented for Qt::DisplayRole only hence it was not able to show the items.
        @QVariant QStringListModel::data(const QModelIndex &index, int role) const
        {
        if (index.row() < 0 || index.row() >= lst.size())
        return QVariant();
        if (role == Qt::DisplayRole || role == Qt::EditRole)
        return lst.at(index.row());
        return QVariant();
        }@
        But with same I'm hit with another problem i.e. my qml shows only the lat item. Any idea how display the entire list?

        1 Reply Last reply
        1

        • Login

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