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

标签 qml qt5 qtquick2

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

主窗口.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

主窗口.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
}

主窗口.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")));
}

...

主窗口.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

我想你会明白的。

关于qml - 如何使用 C++ 实例化一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208100/

相关文章:

qt - 从 TabView 获取事件 Tab 并更改项目属性

c++ - QMetaObject::invokeMethod 无法调用 QML/JS 函数

c++ - 如何在 QML 中访问 C++ 类对象而不是在 QML 中创建单独的对象?

qt - QML Profiler 无法连接到服务器

c++ - 针对 Qt 5.10.1 编译 QtWebKit 5.212 的私有(private) header 问题

c++ - QScintilla - 在自定义词法分析器中为单词添加颜色

c++ - Qt5:如何修改下载速度以显示 1.xx MB/s 而不是 1.xxxxx MB/s?

qt - 如何更改 Tumbler 中所选项目的颜色

c++ - QQuickPaintedItem 的 Paint 方法未调用

c++ - 向 QML 应用程序发送键盘事件