【问题标题】:Enumerate QML properties in Qt C++?在 Qt C++ 中枚举 QML 属性?
【发布时间】:2014-05-23 14:38:55
【问题描述】:

我正在执行以下操作以在我的 Qt 嵌入式应用程序中呈现 QML:

QDeclarativeView *view = new QDeclarativeView(this);
view->setSource(QUrl::fromLocalFile("dial.qml"));
view->show();
QObject *dial = view->rootObject();

有没有办法枚举根项中定义的所有属性值?

例如,如果我有 QML:

import QtQuick 1.0
Item {
  id: root
  property real dial_value : 0
  property real dial_length: 0
  property real background_opacity: 1
  etc, etc
}

是否有一个 Qt 方法最终会让我拥有这些字符串的列表:

dial_value
dial_length
background_opacity

我试过了,但是列表是空的:

QList<QByteArray> list = dial->dynamicPropertyNames();

提前致谢!

【问题讨论】:

  • 你试过dial->metaObject->property(index);在使用 propertyCount() 方法和 name() 方法的循环中?
  • 这样就行了!谢谢你:)

标签: c++ qt qml qtcore qmetaobject


【解决方案1】:

你可以写:

for (int i = 0; i < dial->metaObject->propertyCount(); ++i) {
    QMetaProperty metaProperty = dial->metaobject()->property(i);
    qDebug() << metaProperty.name();
}

【讨论】:

    猜你喜欢
    • 2017-04-23
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2011-02-16
    • 2010-10-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多