如何在被C+修改后从main.qml访问另一个文件的属性+;代码优先 我在QML和C++方面有问题。如果有人能看看这段代码,告诉我我做错了什么。我正试图在一个窗口(主窗口)上打印“msg.author”,但每次我试图从main.qml访问它时,都会出现一个错误,即未定义msg。谢谢你抽出时间 main.qml import QtQuick 2.2 import QtQuick.Window 2.1 Window { property alias name: value visible: true id: main_window width: 500; height: width color:"black" } MyItem.qml import QtQuick 2.0 import QtQuick.Window 2.0 Text { id: text1 visible: true width: 100; height: 100 text: msg.author // invokes Message::author() to get this value color: "green" font.pixelSize: 20 Component.onCompleted: { msg.author = "Jonah" // invokes Message::setAuthor() }

如何在被C+修改后从main.qml访问另一个文件的属性+;代码优先 我在QML和C++方面有问题。如果有人能看看这段代码,告诉我我做错了什么。我正试图在一个窗口(主窗口)上打印“msg.author”,但每次我试图从main.qml访问它时,都会出现一个错误,即未定义msg。谢谢你抽出时间 main.qml import QtQuick 2.2 import QtQuick.Window 2.1 Window { property alias name: value visible: true id: main_window width: 500; height: width color:"black" } MyItem.qml import QtQuick 2.0 import QtQuick.Window 2.0 Text { id: text1 visible: true width: 100; height: 100 text: msg.author // invokes Message::author() to get this value color: "green" font.pixelSize: 20 Component.onCompleted: { msg.author = "Jonah" // invokes Message::setAuthor() },c++,qml,qt5,C++,Qml,Qt5,信息.h #ifndef MESSAGE #define MESSAGE #include <QObject> #include <QString> class Message : public QObject { Q_OBJECT Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged) public: void setAuthor(const Q

信息.h

#ifndef MESSAGE
#define MESSAGE

#include <QObject>
#include <QString>


class Message : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
    void setAuthor(const QString &a) {
        if (a != m_author) {
            m_author = a;
            emit authorChanged();
        }
    }
    QString author() const {
        return m_author;
    }
signals:
    void authorChanged();
private:
    QString m_author;
};

#endif // MESSAGE
#ifndef消息
#定义消息
#包括
#包括
类消息:公共QObject
{
Q_对象
Q_属性(QString author READ author WRITE setAuthor NOTIFY author changed)
公众:
void setAuthor(常量QString&a){
如果(a!=m_作者){
m_author=a;
发出authorChanged();
}
}
QString author()常量{
返回m_作者;
}
信号:
void authorChanged();
私人:
QString m_作者;
};
#endif//消息
main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include <QDebug>
#include "message.h"


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

    QQmlApplicationEngine engine_e;
    engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QQmlEngine engine;
    Message msg;
    engine.rootContext()->setContextProperty("msg", &msg);
    QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));


    if(component.status()!=component.Ready){
        if(component.Error){

        qDebug()<<"Error: "<<component.errorString();
    }
}



    return app.exec();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“message.h”
int main(int argc,char*argv[]){
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
发动机负载(QUrl(QStringLiteral(“qrc:/main.qml”));
QQmlEngine发动机;
消息消息;
engine.rootContext()->setContextProperty(“msg”、&msg);
QQmlComponent组件(&engine,QUrl::fromLocalFile(“main.qml”);
if(component.status()!=component.Ready){
if(组件错误){

qDebug()代码不起作用,并且主引擎有两个
引擎
s。第二个未正确设置,还应注意,从
QQmlApplicationEngine
文档中可以看出:

此类<强>结合QQML引擎和QQMListabs/Stult>提供了一种加载单个QML文件的便捷方法,也暴露了一些对QML的核心应用功能,其中C++ + QML混合应用程序通常会从C++控制。 因此,

QQmlApplicationEngine
可以完全满足您的需要并加载QML文件。现在,还应该注意,在加载QML文件之前,上下文属性应该总是设置为。最后正确的代码如下:

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

    Message msg;
    engine_e.rootContext()->setContextProperty("msg", &msg);

    engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}
编辑 对于与QML文件有关的内容,它们应该放在资源中(我希望您已经这样做了)。我的测试设置如下:

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

    Message msg;
    engine_e.rootContext()->setContextProperty("msg", &msg);

    engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}
main.qml

import QtQuick 2.3
import QtQuick.Window 2.1

Window {

  //  property alias name: value

    visible: true
    id: main_window
    width: 500; height: width
   // color:"black"

    MyItem {
        anchors.centerIn: parent          // <--- added to the main window to see the result
    } 
}

谢谢你的更正。然而,我的主要问题仍然存在。我似乎找不到一种方法来实现这两个“东西”(QML和C++)相互交流。我想知道我是否真的这么笨,或者这项任务真的那么难学!??你有什么错误?我刚刚复制了你的代码,如上所述更正了main,更正了QML代码中的错误(缺少括号)以及所有文件。到底是怎么回事?第一个块(标记为代码)包含两个.qml文件(MyItem.qml和main.qml),现在我正试图在窗口中打印main.qml-msg.author(基本上是Johan的名字)。当我试图访问它时,它会给我一个eroor“msg未定义”.我知道…我刚刚在main中复制了
MyItem
,以查看最终效果。好的,我用我的完整示例进行编辑。现在可以了,谢谢。我必须清理并运行qmake。我将研究一下您的代码。让我们看看我是否可以学到一些东西。