如何使用 C++ 实例化一个元素

How to instantiate an Element using C++

我有一个 class MainWindow 作为不同视图的容器。 MainWindow 继承了 Qml 元素 WindowMainWindow.qml 是主要的 qml 文件。目前看起来像这样:

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();
}

当我尝试调用 changeContent() 时,它显示:TypeError: Property 'changeContent' of object MainWindow_QMLTYPE_1(0x1e8b5c0) is not a function

不在qml文件中实例化MainWindow如何调用呢?

或者:如何让qml根元素知道它是Main.cppMainWindow的实例?


编辑: 所做的更改:

MainWindow.cpp(Header分别改)

...

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();
}

您可以使用 QQmlContext::setContextProperty() 导出已注册类型的实例。

在我的应用程序中调用,例如:

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

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

现在我可以在导入 MyModule 后在 QML 世界中访问 classInstance。 我这里的mMainView继承自QQuickView.

我想你会明白的。