Results 1 to 2 of 2

Thread: QStringListModel doesn’t show on QML ListView.

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: QStringListModel doesn’t show on QML ListView.

    Hi,
    I’m pretty new to QML. I have defined 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:

    Qt Code:
    1. int main(int argc, char ** argv)
    2. {
    3. QGuiApplication app(argc, argv);
    4.  
    5.  
    6. QQuickView view;
    7. QQmlContext *ctxt = view.rootContext();
    8.  
    9. list << "a" << "b" << "c"<<"s"<<"w";
    10. model->setStringList(list);
    11.  
    12.  
    13. ctxt->setContextProperty("myModel", model);
    14.  
    15.  
    16. //![0]
    17.  
    18. view.setSource(QUrl("qrc:view.qml"));
    19. view.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 2.0
    2. //![0]
    3.  
    4. ListView {
    5. width: 100; height: 100
    6.  
    7. model: myModel
    8. delegate: Rectangle {
    9. height: 25
    10. width: 100
    11. Text { text: modelData }
    12.  
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 



    Am missing something? Please suggest!


    Added after 1 27 minutes:


    I solved the problem by using role name as Qt::display in my qml file.
    javascript Code:
    1. import QtQuick 2.0
    2. //![0]
    3.  
    4. ListView {
    5. width: 100; height: 100
    6.  
    7. model: myModel
    8. delegate:
    9. Item{
    10. Rectangle {
    11. height: 25
    12. width: 100
    13. Text { text: display }
    14.  
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 


    QVariant QStringListModel::data(const QModelIndex &index, int role) implemented for Qt:isplayRole only hence it was not able to show the items.

    Qt Code:
    1. QVariant QStringListModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (index.row() < 0 || index.row() >= lst.size())
    4. return QVariant();
    5. if (role == Qt::DisplayRole || role == Qt::EditRole)
    6. return lst.at(index.row());
    7. return QVariant();
    8. }
    To copy to clipboard, switch view to plain text mode 


    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?
    Last edited by wysota; 3rd September 2013 at 19:38. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStringListModel doesn’t show on QML ListView.

    This works quite fine for me without touching QStringListModel:

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. width: 360
    5. height: 360
    6. ListView {
    7. anchors.fill: parent
    8. model: stringlistmodel
    9. delegate: Text { text: display }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    BTW. modelData didn't work for you because it works for QStringList based models (where you expose QStringList to QML) and not for QStringListModel (which is a QAbstractItemModel subclass).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to show the leaf nodes of a treemodel in a listview?
    By Michael_BJFU in forum Qt Programming
    Replies: 4
    Last Post: 10th May 2013, 15:39
  2. Replies: 4
    Last Post: 7th March 2013, 16:20
  3. to show Checkboxes in listview
    By Ketan Shah in forum Newbie
    Replies: 3
    Last Post: 23rd May 2011, 09:01
  4. Replies: 3
    Last Post: 9th December 2010, 21:10
  5. Replies: 2
    Last Post: 21st March 2010, 08:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.