Qt World Summit: Register Today!

{ Qt5.0.2/QML/QtQuick2.0/C++ } Example Projects that run without errors?

  • a duplicate of this question is posted here too: http://stackoverflow.com/questions/16068954/qt5-0-2-qml-qtquick2-0-c-example-projects-that-run-without-errors

    My setup is Qt5.0.2 MinGW 32-bit.

    I am looking for { Qt5.0.2 / QML / QtQuick2.0 / C++ } code project examples (not Qt Quick 1.0) that have actual C++ classes or at least a main.cpp .

    I ran through the web, browsed all examples I could possibly find:

    but they were either old (Qt Quick 1.0) or pure QML+ QtQuick 2.0 or did not compile at all; or if they could compile, they showed me empty dialogs (for example, the "Status Shout!" in the Nokia code examples).

    Any advice would be highly appreciated.

  • I haven't tried with MinGW but I assume you've tested the demos from the qtdeclarative repo?
    e.g., http://qt.gitorious.org/qt/qtdeclarative/trees/stable/examples/quick/demos/maroon


  • Hi Chris! :)

    I opened the Maroon project and it is pretty fun :) But it is QML + .js for the code logic. I need QML + C++.

    P.S. I opened all other projects in qtdeclarative - they are all the same, I did not find one that interacts with C++

  • It has a main.cpp entrypoint. If you want to add C++ object interaction, well, try adding some - follow the docs advice (either register your types via qmlRegisterType(), or insert a QObject into the root context as a context property).


  • OK, I am trying to do that but I am totally messed up with the advices, the docs and the guidelines as they are for different versions of Qt. This is why I am looking for a complete sample project that does that.

  • C++ classes for Quick2 are renamed from QDeclarative* to QQml* or QQuick* depending on which module they belong to. For example QDeclarativeView is QQuickView and QDeclarativeEngine is QQmlEngine. Otherwise the API should be same as in Quick 1.

    Abstractitemmodel example has C++ and QML interaction. Example can be found in Qt5 source tree in "qtdeclarative/examples/quick/models/abstractitemmodel"

  • Were you looking at the correct docs? The Qt 5 stable doc snapshot includes extensive documentation on QML/C++ integration:


  • Thank you Tomma and Chris!

    Tomma, I checked all the examples. But they were basically using JavaScript to fill in their GUI data. I am trying to fill in the data into the GUI from C++. Maybe I am not looking in the right place and it is my mistake.

    Chris, I read the link. What I am trying to do exactly is the following: I want to get and set the value of QML string list from C++ ( to fill in the QML GUI with data from C++). My GUI works but my attempts to fill it with data fail.


    • I have a QML application using QuickView 2.0/Qt 5.0. When I create the project, I choose File->New->Application->Qt Quick 2 Application.
    • I have several QML files, one of which includes the others. So I have a ComboBox QML type nested inside my main.qml
      in main.qml:

    Page1 {
    Page1Left {
    ComboBox {
    objectName : "selectType"

    Inside the ComboBox.qml I have :
    @ComboBox {
    property variant items: ["Item 1", "Item 2", "Item 3"]

    In .cpp for Qt 5.0, I visit the following "help page":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-contextproperties.html#setting-a-simple-context-property where it reads "Setting a Simple Context Property" and I follow the steps.

    It says that I need to use the following function:
    @QQuickView view;
    view.rootContext()->setContextProperty("currentDateTime", QDateTime::currentDateTime());

    And I follow the advice:
    @viewer = new QQuickView;

    all ok, but then viewer->rootContext() does not have a method called 'setContextProperty'

    _Besides, almost all examples in the docs use QuickView instead of QtQuick2ApplicationViewer. This also confuses me as to why the automatically generated project uses QtQuick2ApplicationViewer and not QuickView. _

    I also wrote a small class like this:
    @ class Thing : public QObject

    Q_PROPERTY (QStringList items READ getItems NOTIFY itemsChanged )

    Thing(QObject * parent = NULL) : QObject(parent) {m_items << "0 dfds" << "1 dfds" <<"4" ; /initial dummy data/}

    Q_INVOKABLE QStringList getItems() {return m_items;}
    Q_INVOKABLE void addItems(QStringList newItems) { m_items += newItems;}

    void itemsChanged();

    QStringList m_items;

    And I am trying to pass the array from this object to the QML list named 'items'. Basically in QML I want to write @ property variant items: Thing.getItems /or Thing.iems - whatever works/ @

    As Tomma said, I am totally avoiding everything close to the name 'QDeclarative' - it seems to be from the older version of Qt, before 5.0.

  • Hi,

    Hrm, the docs exist, but aren't linked to from the article which I linked you before, unfortunately.

    See http://doc-snapshot.qt-project.org/qt5-stable/qtquick/qtquick-modelviewsdata-modelview.html
    and http://doc-snapshot.qt-project.org/qt5-stable/qtquick/qtquick-modelviewsdata-cppmodels.html for more information on using C++ models with QtQuick views.

    It seems that a lot of the snippets are broken currently... and I don't know why.
    I guess the doc team have made some changes to the structure which causes snippets to fail. I'll ping Jerome about it on IRC.


  • A lot of missing code in that last link. And to think 5.0.1 was supposed to finally fix the documentation. Back in the Qt4 days I was really used to having excellent Qt documentation and I often cited the doc as one of Qt's strong points, but every since Qt5 there are a lot of features with missing or no documentation at all.

  • I have a project that has extensive amount of c++/qquick2/javascript. Few points:


    1. Perhaps you should use 'var' like also in javascript in your generic types:
      @property var items: ["Item 1", "Item 2", "Item 3"]@

    At least it works for me very well.

    1. If setting contextProperty does not work, then try to include QQmlContext
      @#include <QQmlContext>@
      and perhaps explicitly make that QDateTime as QVariant:
      @view.rootContext()->setContextProperty("currentDateTime", QVariant(QDateTime::currentDateTime()));@

    2. QStringList is not part of the supported types (in that same link), so I believe m_items from c++ won't get placed into items in qml. You could (again) try to make that getItems() to return QVariant:
      @QVariant getItems() { return QVariant(m_items); }@

    3. Your addItems() does not emit itemsChanged(). That's bad.

    4. By given information, this can not work:
      @property var items: Thing.getItems@

    The idea is correct: you want to read items from Thing. But the case is, you need the object reference not class. So if you create the new thing and then put it into contextProperty, it shall work:
    @Thing* oneThing = new Thing();
    view.rootContext()->setContextProperty("thing", oneThing);@
    @property var items: thing.items@

    Notice, that the object name 'thing' is the same as in qml and the first parameter for setContextProperty(). And thing.items works, because you have that Q_PROPERTY set for reading the list. Although you might want to change the type into QVariant too.

    I hope this helps a bit.

  • QStringList is a supported type. The subset of types known as "sequence types" have conversions to JS types - although the conversion is "imperfect" (as documented, it doesn't create properly sparse arrays, as C++ has no analogue). Sequence types include QStringList, QList<QString>, QList<QUrl>, QList<int>, QList<real>.

Log in to reply