簡體   English   中英

QML 中的附加屬性

[英]Attached properties in QML

我可以像組件一樣為所有屬性創建自己的附件嗎?

Item{
    Component.onCompleted : {} // Component is attached to everyone Items
}

您可能無法將屬性附加到您未創建的項目或組件。 但是你為什么要這樣做呢?

相反,您可以考慮使用信號和全局屬性。

對於可以從任何地方訪問的全局屬性,您可以設置聲明性視圖根上下文上下文屬性

IE,

QmlApplicationViewer viewer;
MyPropertyClass myProp;
viewer->rootContext()->setContextProperty("MyPropClass", &myProp);

現在,在您的 QML 文件中,您可以訪問此類的屬性作為

Rectangle {
    Text {
        text: MyPropClass.getMyPropText()
    }
    MouseArea {
        onClicked: { MyPropClass.text = "Clicked" }
    }
}

這將從 MyPropertyClass 調用 Q_INVOKABLE 方法 getMyPropText()。 和 Q_PROPERTY 'text' 可以在發出某些信號時設置。

這是否適合您的需要?

是的,例如:

#include <QQmlEngine>
#include <QTimer>

class TestAttached : public QObject
{
    Q_OBJECT
    // Declare property with setter and getter
    Q_PROPERTY(int val READ getVal WRITE setVal)
public:
    TestAttached() Q_DECL_EQ_DEFAULT;
    explicit TestAttached(QObject *parent):
        QObject{parent},
        m_val{100500}
    {
        Q_ASSERT(parent);
        qDebug() << "* We just have created the object of attached properties for" << parent->metaObject()->className();
    }
    ~TestAttached() Q_DECL_EQ_DEFAULT;
    
    // define static function qmlAttachedProperties(QObject *object)
    // which returns pointer to instance of TestAttached class
    static TestAttached *qmlAttachedProperties(QObject *object)
    {
        TestAttached* testAttached { new TestAttached{object} };
        QTimer* timer { new QTimer{testAttached} };
        connect(timer, &QTimer::timeout,
        [testAttached] {
            testAttached->setVal(testAttached->getVal()+1);
            emit testAttached->testDone(testAttached->getVal());
        });
        timer->start(3000);
        return testAttached;
    }

    inline int getVal() const
    {
        return m_val;
    }
    inline void setVal(int val)
    {
        m_val = val;
    }

signals:
    void testDone(int val);

private:
    int m_val;
};
// Makes the type TestAttached known to QMetaType (for using with QVariant)
QML_DECLARE_TYPE(TestAttached)
// declares that the TestAttached supports attached properties.
QML_DECLARE_TYPEINFO(TestAttached, QML_HAS_ATTACHED_PROPERTIES)

在 QML 系統中注冊附加類型(例如在main.cpp ):

qmlRegisterUncreatableType<TestAttached>("my.test", 1, 0, "Test", QObject::tr("Test is an abstract type that is only available as an attached property."));

並在main.qml嘗試:

import QtQuick 2.15
import QtQuick.Window 2.15
//import our module
import my.test 1.0

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

    Rectangle {
        color: focus ? Qt.lighter("red") : "red"
        width: parent.width/2; height: parent.height
        anchors.left: parent.left
        Component.onCompleted: {
            console.log("Rectangle is completed", Test.val)
        }
        Keys.onReturnPressed: {
            console.log("Rect 1: Attachet value is", Test.val)
        }

        Test.val: 20000

        Test.onTestDone: function(num) {
            console.log("We received", num, "from attached object")
        }

        MouseArea {
            anchors.fill: parent
            onClicked: parent.focus = true
        }
    }

    Rectangle {
        color: focus ? Qt.lighter("blue") : "blue"
        width: parent.width/2; height: parent.height
        anchors.right: parent.right
        focus: true

        Keys.onReturnPressed: {
            // The attached object will created after return pressed
            console.log("Rect 2: Attachet value is", Test.val)
        }

        MouseArea {
            anchors.fill: parent
            onClicked: parent.focus = true
        }
    }
}

暫無
暫無

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

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