简体   繁体   English

如何在QtQuick2Con​​trolsApplicationViewer上绑定QML / C ++类

[英]How to bind QML /C++ class on QtQuick2ControlsApplicationViewer

Currently I'm developing QtQuick application by QtCreator3.0.0, Qt5.2.0, and MSVC2012. 目前,我正在通过QtCreator3.0.0,Qt5.2.0和MSVC2012开发QtQuick应用程序。 I want to make an application that calls C++ function from qml. 我想制作一个从qml调用C ++函数的应用程序。 Based on this article . 基于本文 it works when I use QtQuick2ApplicationViewer for QtQuick2 application. 当我将QtQuick2ApplicationViewer用于QtQuick2应用程序时,它可以工作。

[main.cpp] [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. 对于QtQuick控件应用程序,它需要QtQuick2Con​​trolsApplicationViewer而不是QtQuick2ApplicationViewer。 But it doesn't have the method "rootContext()". 但是它没有方法“ rootContext()”。

How should I bind QML and C++ class in this case. 在这种情况下,我应该如何绑定QML和C ++类。

我从Qt项目论坛获得了解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM