[Interest] Context ambiguities when exposing a Qt Stringlist to QML

Willem Ferguson willemferguson at zoology.up.ac.za
Tue Jan 5 20:00:14 CET 2016



Here are QML and C++ snippets. The root context provides a context to 
the top-level QML rectangle. But the QStringList is required by the 
grandchild of the top-level rectangle. I suspect the context for 
exposing the QStringList is inappropriate. What is the correct approach 
here?

C++:

int  main(int  argc,  char  **  argv)
{
     QGuiApplication <http://doc.qt.io/qt-5/qguiapplication.html>  app(argc,  argv);

     QStringList <http://doc.qt.io/qt-5/qstringlist.html>  dataList;
     dataList.append("Apples");
     dataList.append("Oranges");
     dataList.append("Bananas");
     dataList.append("Grapes");

     QQuickView <http://doc.qt.io/qt-5/qquickview.html>  view;
     QQmlContext <http://doc.qt.io/qt-5/qqmlcontext.html>  *ctxt=  view.rootContext();
     ctxt->setContextProperty("myModel",  QVariant <http://doc.qt.io/qt-5/qvariant.html>::fromValue(dataList));

     view.setSource(QUrl <http://doc.qt.io/qt-5/qurl.html>("qrc:view.qml"));
     view.show();

     return  app.exec();
}

QML:

import QtQuick 2.0

Rectangle { <http://doc.qt.io/qt-5/qml-qtquick-listview.html> width: 
400; height 400 Rectangle { width: 200; height 200 ListView 
<http://doc.qt.io/qt-5/qml-qtquick-listview.html>  {
     width:100;height:100
             model:myModel
             delegate:Rectangle  {
                 height:25;  width:100
             Text <http://doc.qt.io/qt-5/qml-qtquick-text.html>  {text:modelData  }
             }
         }

    }
}

ctxt in the C++ code relates to the outer bounding rectangle, thus the 
context of myModel is inappropriate with respect to the ListView item. 
Am I correct? If so, how should one correct this? Any feedback will be 
highly appreciated. The QQmlContext documentation gives no indication 
about solutions for this.
Kind regards,
Willem




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160105/3b3e94ee/attachment.html>


More information about the Interest mailing list