QML 中的附加属性
Attached properties in QML
我可以为所有属性(如组件)创建自己的附加属性吗?
Item{
Component.onCompleted : {} // Component is attached to everyone Items
}
是的
,例如:
#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
}
}
}
您可能无法将属性附加到并非创建的条目或组件。但是你为什么要这样做呢?
相反,您可以考虑使用信号和全局属性。
对于可从任何位置访问的全局属性,可以设置声明性视图根上下文的上下文属性。
即,
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发出某些信号时可以设置"文本"。
这适合您需要吗?
相关文章:
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- Qt Quick-如何仅从c++代码与qml属性交互
- 如何将类的属性从 c++ 获取到 QML 中
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- 属性分配无效:"displayText"是文本字段 qml 中的只读属性
- QVariants 在 QML 中的C++和属性中的使用
- 在 QML 中更改父属性时更改子属性
- QML / C++可选属性
- QML 检测到属性宽度的绑定循环(TextMetrics 行为很奇怪)
- 可以从C++在QML上编写上下文属性吗?
- 当删除上下文属性中的QLIST对象时,QT QML应用程序崩溃
- 如何从 c++ 更改 QML 项的属性
- 使用 QAbstractListModel 分配 QML 布尔属性失败
- QML 无法分配给不存在的属性
- 从C++使用编译时安全性更改 QML 属性
- Javascript 函数作为从 c++ 定义的 QML 属性
- 从高度动态的C 数据模型中更新QML:计时器与属性绑定
- 从C 读取QML数组属性
- 如何使用 API 获取 QML 对象的 id 属性C++
- Qt-Qml连接到上下文属性的QObject属性的信号