簡體   English   中英

從C ++在QML中設置對象類型屬性

[英]Setting object type property in QML from C++

(已編輯以添加更多上下文)

我已經開始使用QML,並且想在QML類型上設置某種引用屬性,以鏈接兩個QML對象(理想情況下,沒有父/子關系,因為我想連接多個QML對象)。

例如,我有以下文件。

qmldir:

A 1.0 A.qml

A.qml

import QtQuick 2.2

Rectangle {
    width: 100
    height: 100
    color: 'red'
    // Other stuff later
}

main.qml:

import QtQuick 2.2
import QtQuick.Window 2.1
import "qrc:/"

Rectangle {
    objectName: "Main window"
    visible: true
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    property A referencedObject;

    Rectangle {
        id: subView
        objectName: "subView"
    }
}

我要做的是將'referencedObject'的值設置為C ++在運行時創建的對象。 但是,用於設置屬性的函數不允許使用QObject指針或QQuickItem指針,因為無法以這種方式構造QVariant對象。

就像是:

QQmlComponent aTemplate(&engine, QString("qrc:/A.qml"), &parentObject);
QQuickItem* aInstance = aTemplate.create();

aInstance->setParent(&parentObject);
mainView.setProperty("referencedObject",aInstance); // Won't work.

我想在QML中保留'A'類型的對象,因為a)為此,它比C ++的樣板要少,並且b)意味着它是一個具有生命周期的獨立圖形對象,並且QML在該用例下效果更好。

感謝您發布的任何幫助。

以下示例顯示了如何設置從C ++在QML中定義的自定義類型的對象的屬性。

A.qml

import QtQuick 2.2
Rectangle {
    width: 0
    height: 0
    color:"red"
}

main.qml

import QtQuick 2.2  
Rectangle {
    visible: true
    width: 640
    height: 480

    property alias referencedObject:aProperty;

    A
    {
        id:aProperty
        objectName: "aPropertyObject"//Needed to access it from C++
    }
}

現在,我們定義了qml類型A main.qml具有此“自定義”類型的屬性。 我們需要從C ++更改此對象的屬性。 讓我們更改寬度,高度和顏色。

main.cpp中

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QQmlComponent aTemplate(&engine, QUrl(("qrc:///A.qml")));
    QQuickItem* aInstance =qobject_cast<QQuickItem*>(aTemplate.create());
    aInstance->setWidth(100);
    aInstance->setHeight(100);
    aInstance->setProperty("color",QColor("green"));
    if(aInstance)
    {
        QQuickView *mainView = new QQuickView;
        mainView->setSource(QUrl(QStringLiteral("qrc:///main.qml")));
        mainView->show();
        QQuickItem * aPropertyObject = mainView->rootObject()->findChild<QQuickItem*>("aPropertyObject");
        if(aPropertyObject)
        {
           //Now you have pointers to both source and destination.
           //You can write a helper function which assigns the values
           //of source to the destination.
           //For the sake of demonstration, I am just setting some properties.       
           aPropertyObject->setWidth(aInstance->width());
           aPropertyObject->setHeight(aInstance->height());
           aPropertyObject->setProperty("color",aInstance->property("color"));
        }
    }
    return app.exec();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM