2
votes

Currently I'm developing QtQuick application by QtCreator3.0.0, Qt5.2.0, and MSVC2012. I want to make an application that calls C++ function from qml. Based on this article. it works when I use QtQuick2ApplicationViewer for QtQuick2 application.

[main.cpp]

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include "testclass.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    TestClass cppObj;
    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("cppObj", &cppObj);
    viewer.setMainQmlFile(QStringLiteral("qml/QtQuick2CppTest/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

For QtQuick Controls application, it requires QtQuick2ControlsApplicationViewer instead of QtQuick2ApplicationViewer. But it doesn't have the method "rootContext()".

How should I bind QML and C++ class in this case.

1

1 Answers

1
votes