Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to bind QML /C++ class on QtQuick2ControlsApplicationViewer
Forum Updated to NodeBB v4.3 + New Features

How to bind QML /C++ class on QtQuick2ControlsApplicationViewer

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yufit_in_Japan
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yufit_in_Japan
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          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

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yufit_in_Japan
            wrote on last edited by
            #5

            @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:)

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              You're welcome :)

              Happy coding!

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved