How to bind QML /C++ class on QtQuick2ControlsApplicationViewer



  • 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":http://qt-project.org/forums/viewthread/22685, it works when I use QtQuick2ApplicationViewer for QtQuick2 application.

    @
    #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.


  • Moderators

    Hi,

    -Qt Quick Controls is just another module that contains Qt Quick items. You can still use QtQuick2ApplicationViewer.-



  • Thank you for your advice.

    [quote author="JKSH" date="1390653249"]Hi,

    Qt Quick Controls is just another module that contains Qt Quick items. You can still use QtQuick2ApplicationViewer.
    [/quote]

    When "ApplicationWindow" element is used in my application, it doesn't work on QtQuick2ApplicationViewer. According to "this article":http://stackoverflow.com/questions/21311951/how-to-run-an-application-which-import-qtquick2-0-and-qtquick-controls-1-1, I was advised that QtQuick2ControlsApplicationViewer should be used for QtQuick Controls application.
    Did I misunderstand something?


  • Moderators

    My apologies, I was mistaken. You're right, ApplicationWindow cannot be used with QtQuick2ApplicationViewer which subclasses QQuickView.

    Ok, note that QtQuick2ControlsApplicationViewer is not actually part of Qt. Its code is auto-generated when you create a new Qt Quick Controls project. That means you can modify QtQuick2ControlsApplicationViewer.

    The code generator forgot to provide a function to get the root context. You can add it yourself:

    @
    QQmlContext* QtQuick2ControlsApplicationViewer::rootContext() {
    return d->engine.rootContext();
    }
    @

    There is a bug report that describes this oversight: https://bugreports.qt-project.org/browse/QTCREATORBUG-11048



  • @JKSH
    Thanks to your polite response , my application works finally.

    Actually I didn't notice that QtQuick2ControlsApplicationViewer is automatically generated by QtCreator project wizard until I had read your comment.

    My problem has been completely solved:)


  • Moderators

    You're welcome :)

    Happy coding!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.