如何在主要由C ++代码修改后从main.qml访问另一个文件的属性

时间:2014-12-18 21:15:56

标签: c++ qml qt5

我遇到QML和C ++问题。如果有人可以看看这段代码并告诉我我做错了什么。我只是想在窗口(主窗口)上打印“msg.author”,但每次我尝试从main.qml访问它时都会出现错误msg is not defined。谢谢你的时间。

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

message.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

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

1 个答案:

答案 0 :(得分:0)

代码无效,主要有两个engine。第二个没有正确设置,还应该从QQmlApplicationEngine文档中注意到:

  

此类结合了QQmlEngine和QQmlComponent ,以提供加载单个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文件的问题,它们应该放在资源中(我希望你已经完成了)。我的测试设置如下:

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
    } 
}

MyItem.qml

import QtQuick 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()
    }
}