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. [SOLVED] QtQuick2ControlsApplicationViewer connect signal to slot
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QtQuick2ControlsApplicationViewer connect signal to slot

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.5k 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.
  • H Offline
    H Offline
    Hawk30100
    wrote on last edited by
    #1

    Hello there,

    I want to connect a qml signal to a c++ slot via QObject::connect() in my main.cpp.
    I'm understanding how to connect a signal to a slot.
    Normally I need something like this: QObject::connect(viewer.rootObject(), SIGNAL(foo()), &targetClass, SLOT(foo()));

    My problem is to get the rootObject of my view.
    I just saw many solutions with QtQuick2ApplicationViewer. They are all using the rootObject-method.
    QtQuick2ControlsApplicationViewer does not have the method rootObject().

    Few weeks ago I found out that QtQuick2ControlsApplicationViewer is missing the rootContext()-method when creating a QtQuick2Controls project with the Wizard using Qt Creator 3.0.1. I just added the missing lines by myself into the .h and .cpp file.
    (The bug was already reported and it was fixed in Qt Creator 3.1)

    Here is the code of my main.cpp:
    (I just removed some boring lines of code)
    @#include "qtquick2controlsapplicationviewer.h"
    #include "Storage/storage.h"

    #include <QQmlContext>
    #include <QtQuick>
    #include <QtGui/QGuiApplication>

    int main(int argc, char *argv[])
    {

    Q_INIT_RESOURCE(resources);
    Storage storage;
    
    QtQuick2ControlsApplicationViewer viewer;
    
    viewer.rootContext()->setContextProperty("storage", &storage);
    

    QObject::connect((QObject*)viewer.rootObject(), SIGNAL(getLastState()), &storage, SLOT(getLastState()));

    viewer.setMainQmlFile(QStringLiteral("qml/App/main.qml"&#41;&#41;;
    viewer.show(&#41;;
    
    return app.exec(&#41;;
    

    }
    @

    Is the method rootObject() missing in my project or does it not exists?
    How can I connect a qml signal with a c++ slot in my QtQuick2ControlsApplication main.cpp?

    Line 18 is not working, viewer.rootObject() method does not exists for QtQuick2ControlsApplicationViewer objects.
    By the way... is it possible that there is no documentation about QtQuick2ControlsApplicationViewer ??

    Thanks in advance!

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

      Hi,

      QtQuick2ControlsApplication is not part of Qt. It is only a convenience class that wraps a QQmlEngine and a QQuickWindow. Its code is auto-generated by Qt Creator, that's why there is no documentation. Unfortunately, as you have discovered, it is missing some important functions.

      I recommend updating to Qt Creator 3.1 -- QtQuick2ControlsApplicationViewer has been removed, and is replaced with "QQmlApplicationEngine":http://qt-project.org/doc/qt-5/qqmlapplicationengine.html. You can then easily call QQmlApplicationEngine::rootObjects().

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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hawk30100
        wrote on last edited by
        #3

        Thanks!

        I just did what you said. Installed Qt Creator 3.1 and used the engine class. Works perfect!

        I also manually transfered all data from my old projekt into a new one.
        Just saw that some new files will be added to the project if you create a new project within Qt Creator 3.1. "qtquick2controlsapplicationviewer" was replaced with "deployment" folder and the qml-files(quick2) will be added to the resources folder instead of the QML folder.

        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