简体   繁体   English

从 QQuickView 对象设置 QML 项的父属性

[英]Set a parent property of a QML item from a QQuickView object

I have 3 files (along with CMakeLists.txt and qrc ):我有 3 个文件(连同CMakeLists.txtqrc ):

  • main.cpp - C++ main.cpp - C++
  • BDialog.qml - a "base" for my dialog (contains the property definition) BDialog.qml - 我的对话框的“基础”(包含属性定义)
  • BEditorDialog.qml - a dialog with a label BEditorDialog.qml - 带有标签的对话框

They contain:他们包含:

// main.cpp

#include <QApplication>
#include <QQuickView>
#include <QQmlContext>


int main( int argc, char **argv )
{
    QApplication app( argc, argv );

    QQuickView view;
    view.rootContext()->setContextProperty( "myText", "WOW!" ); // expected to work but doesn't

    view.setSource( QUrl( "qrc:/BEditorDialog.qml" ) );

    view.show();
    return app.exec();
}

//BDialog.qml

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

Item {
    // This property I want to set from C++
    property string myText
}

// BEditorDialog.qml

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

BDialog {
    id: root

    Label {
        id: label
        text: root.myText // doesn't work
    }
}

If I assign initial value to the myText property:如果我将初始值分配给myText属性:

property string myText: "MEH!"

this initial value is displayed, but how to assign it from C++?显示了这个初始值,但如何从 C++ 分配它? I suspect I should use the setInitialProperties member function, but I have only Qt 5.12 installed.我怀疑我应该使用setInitialProperties成员函数,但我只安装了 Qt 5.12。

Thank you in advance.先感谢您。

You've mixed up context and object.您混淆了上下文和对象。 You're setting the property "myText" on the QQmlContext which is not the root object of your application.您正在QQmlContext上设置属性"myText" ,它不是应用程序的根对象。 To get the root object you can use rootObject() on your QQuickView and get a QQuickItem back.要获取根对象,您可以在QQuickView上使用rootObject()并取回QQuickItem On that you can call setProperty() .在上面你可以调用setProperty()

This works这有效

#include <QApplication>
#include <QQmlContext>
#include <QQuickItem>
#include <QQuickView>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QQuickView view;
    view.setSource(QUrl("qrc:/BEditorDialog.qml"));
    // First set the source and then access the rootObject, otherwise it isn't create yet
    view.rootObject()->setProperty("myText", "WOW!");

    view.show();
    return app.exec();
}

In addition to the answer of iam_peter, you can actually also use the context property in the way you have set it, but with the warning that a name collision is present.除了 iam_peter 的答案之外,您实际上还可以按照您设置的方式使用上下文属性,但会出现名称冲突的警告。 This might be more appropriate in bigger projects (it seems here you only want a way to show a dialog from C++ and be done with it)这在更大的项目中可能更合适(在这里你似乎只想要一种方法来显示 C++ 的对话框并完成它)

Given a proper name, say 'myDialogText', you can use it as follows:给定一个专有名称,例如“myDialogText”,您可以按如下方式使用它:

// BEditorDialog.qml

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

BDialog {
    id: root

    Label {
        id: label
        text: myDialogText
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM