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

Dmitry Volosnykh dmitry.volosnykh at gmail.com
Tue Jan 5 20:06:17 CET 2016


Documentation states:
"There are two ways in which such sequences can be exposed to QML: as a
Q_PROPERTY <http://doc.qt.io/qt-5/qobject.html#Q_PROPERTY> of the given
sequence type; or as the return type of a Q_INVOKABLE
<http://doc.qt.io/qt-5/qobject.html#Q_INVOKABLE> method." Nothing is being
said about global context injection. For more information on data type
conversion between C++ and QML please refer to
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array

On Tue, Jan 5, 2016 at 10:00 PM Willem Ferguson <
willemferguson at zoology.up.ac.za> wrote:

>
>
> 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
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160105/01cecd05/attachment.html>


More information about the Interest mailing list