SimonH
SimonH

Reputation: 1415

How to instantiate an Element using C++

I have a class MainWindow as a container for different views. MainWindow inherits the Qml element Window. MainWindow.qml is the main qml file. Currently it looks like this:

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QQmlApplicationEngine>
#include <string>
#include "CustomMenu.h"

class MainWindow : public QObject
{
    Q_OBJECT
private:
    QQmlApplicationEngine engine_;
    QObject* content_;

public:
    MainWindow(QObject* parent = 0);
    Q_INVOKABLE void changeContent(std::string contentID);
};

#endif // MAINWINDOW_H

MainWindow.cpp:

#include "MainWindow.h"
#include <QQmlComponent>
#include <QObject>
#include <QtQml>
#include <iostream>

// Legt ein neues MainWindow-Objekt an und registriert die eigenen Typen für QML
MainWindow::MainWindow(QObject* parent)
    : QObject(parent), engine_(new QQmlApplicationEngine())
{
    std::cout << "in mainwindow ctor" << std::endl;

    // Registriere Typen für QML
    qmlRegisterType<MainWindow>("com.Gui", 1, 0, "MainWindow");
    qmlRegisterType<CustomMenu>("com.Gui", 1, 0, "StartMenu");
    qmlRegisterType<CustomMenu>("com.Gui", 1, 0, "HelpMenu");

    // Lade MainWindow
    engine_.load(QUrl(QStringLiteral("qrc:/Gui/MainWindow.qml")));
}

void MainWindow::changeContent(std::string contentID)
{
    // not implemented yet
}

MainWindow.qml:

import QtQuick 2.3
import QtQuick.Window 2.2
import com.Gui 1.0

Window {
    visible: true
    id: main
    width: 1024
    height: 768
    property string contentID: "startMenu"
    property CustomMenu content: null

    MouseArea {
        anchors.fill: parent
        onClicked: {
            main.changeContent("StartMenu")
        }
    }
}

Main.cpp

#include <QGuiApplication>
#include "Gui/MainWindow.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    MainWindow* mainwindow = new MainWindow();

    return app.exec();
}

When I try to call changeContent() it says: TypeError: Property 'changeContent' of object MainWindow_QMLTYPE_1(0x1e8b5c0) is not a function.

How can I call it without instantiating MainWindow in the qml file?

Or: How can I let the qml root Element know that it is the instance of MainWindow in Main.cpp?


edit: Changes made:

MainWindow.cpp (Header respectively changed)

...

MainWindow::MainWindow(QWindow* parent)
    : QQuickView(parent), engine_(new QQmlApplicationEngine())
{
    std::cout << "in mainwindow ctor" << std::endl;
}

void MainWindow::show()
{
    // Registriere Typen für QML
    qmlRegisterType<MainWindow>("com.Gui", 1, 0, "MainWindow");
    qmlRegisterType<CustomMenu>("com.Gui", 1, 0, "StartMenu");
    qmlRegisterType<CustomMenu>("com.Gui", 1, 0, "HelpMenu");

    // Setze diese Instanz als Rootobjekt in QML
    this->rootContext()->setContextProperty("mainWindowInstance", this);

    // Lade MainWindow
    engine_.load(QUrl(QStringLiteral("qrc:/Gui/MainWindow.qml")));
}

...

MainWindow.qml

import QtQuick 2.3
import QtQuick.Window 2.2
import com.Gui 1.0

Window {
    visible: true
    id: main
    width: 1024
    height: 768
    property string contentID: "startMenu"
    property CustomMenu content: null
    property MainWindow mainWindowInstance

    MouseArea {
        anchors.fill: parent
        onClicked: {
            mainWindowInstance.changeContent("StartMenu")
        }
    }
}

Main.cpp

#include <QGuiApplication>
#include "Gui/MainWindow.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    MainWindow* mainwindow = new MainWindow();
    mainwindow->show();

    return app.exec();
}

Upvotes: 3

Views: 144

Answers (1)

juzzlin
juzzlin

Reputation: 47875

You can export instances of your registered types with QQmlContext::setContextProperty().

In my app I call, for example:

qmlRegisterType<MyClass>("MyModule", 1, 0, "MyClass");

MyClass* mClassInstance = new MyClass;
mMainView->rootContext()->setContextProperty("classInstance", mClassInstance);

Now I can access classInstance in QML world after importing MyModule. My mMainView here is inherited from QQuickView.

I think you'll get the idea.

Upvotes: 1

Related Questions