gpt4 book ai didi

c++ - 从 C++ 实例化 QML

转载 作者:行者123 更新时间:2023-11-28 04:30:07 25 4
gpt4 key购买 nike

我有 2 个类,MyApp 和 MyAppView。 MyApp 类将包含其他类,实现将在这里。 (您可以称它为 Manager 类或 System 类)。 MyAppView 类只与 main.qml 交互,就像它有很多“Q_PROPERTY”。我想你明白这一点。我不希望 MyApp 有“Q_PROPERTY”。

场景如下;

//------------------------------------
//---------------------------main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myapp.h"
#include "myappview.h"

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

qmlRegisterType<MyAppView>("org.myappview", 1, 0, "MyAppView");

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

MyApp myApp;

return app.exec();
}



//------------------------------------
//---------------------------myappview.h
#include <QObject>

class MyAppView : QObject
{
Q_OBJECT
Q_PROPERTY(QString myString READ getMyString NOTIFY myStringChanged)

public:
MyAppView();
QString getMyString() { return m_myString; }
void setMyString(QString newString)
{
m_myString = newString;
emit myStringChanged;
}

signals:
void myStringChanged();

private:
QString m_myString;
}



//------------------------------------
//---------------------------main.qml
import QtQuick 2.0
import QtQuick.Window 2.0

import org.myappview 1.0

Window {
visible: true

MyAppView {
id: backend
}

Text {
text: qsTr(backend.myString)
}
}



//------------------------------------
//---------------------------myapp.h
#include <QObject>
#include "myappview.h"

class MyApp : QObject
{
Q_OBJECT
public:
MyApp();

private:
MyAppView appView;
void changeMyStringInAppView()
{
// This will automatically update main.qml
appView.setMyString("This is new string");
}
}

也可以从 MyApp 访问现有的 QML 实例,而不是从 MyApp 实例化 QML。所以重点是从 Manager 类实例化或到达 View 类,以便我可以轻松控制它。也许在某些方面,我的逻辑是错误的。如果我是,请告诉我。我接受所有建议。

最佳答案

您的代码中的问题是 MyAppMyAppView 与在 QML 中创建的不同,因此如果您更新 appView text 它不会反射(reflect)在 backend 文本中,所以解决方案是使用 setContextProperty() 将一个对象从 MyApp 公开给 QML 并将调用一个函数来建立在 QML 中创建的 MyAppView(记住只创建一个 MyApp 但你会遇到同样的问题)

// myappview.h

#ifndef MYAPPVIEW_H
#define MYAPPVIEW_H

#include <QObject>

class MyAppView : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myString READ getMyString NOTIFY myStringChanged)
public:
explicit MyAppView(QObject *parent = nullptr) : QObject(parent)
{}
QString getMyString() const { return m_myString; }
void setMyString(const QString & newString)
{
if(m_myString != newString){
m_myString = newString;
emit myStringChanged();
}
}
signals:
void myStringChanged();
private:
QString m_myString;
};

#endif // MYAPPVIEW_H

// myapp.h

#ifndef MYAPP_H
#define MYAPP_H

#include "myappview.h"
#include <QObject>

class MyApp : public QObject
{
Q_OBJECT
public:
explicit MyApp(QObject *parent = nullptr) : QObject(parent),
appView(nullptr)
{}
Q_INVOKABLE void setAppView(MyAppView *value){
appView = value;
}
void changeMyStringInAppView()
{
if(appView)
appView->setMyString("This is new string");
}
private:
MyAppView *appView;
};

#endif // MYAPP_H

// main.cpp

#include "myapp.h"
#include "myappview.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

QGuiApplication app(argc, argv);
qmlRegisterType<MyAppView>("org.myappview", 1, 0, "MyAppView");

MyApp myapp;
QTimer::singleShot(1000, &myapp, &MyApp::changeMyStringInAppView);

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myapp", &myapp);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}

// main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import org.myappview 1.0

Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")

MyAppView {
id: backend
}

Text {
text: qsTr(backend.myString)
}
Component.onCompleted: myapp.setAppView(backend)
}

关于c++ - 从 C++ 实例化 QML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53174694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com