qt - Set property value of QObject by index, not name
问题描述
In my publisher-subscriber class the Qt components subscribe by their property names. The publisher maps the pairs <QObject*,PropertyName (as QString)>
to the names of the publishable variables.
{ VarName -> [(QObject*, PropName)] }
On variable change, the list of subscribed QObjects are called using setProperty:
subscriber->setProperty( PropName.toAscii().constData(), NewValue );
I'd like to optimize the conversion from QString to char*. Also I assume, internally in setProperty the property setter function is found by going through the list of const* and string compare.
QMetaObject provides the method:
int QMetaObject::indexOfProperty(const char *name) const
which I could use during the subscription to get the index and later on the value change use only Index instead of the string-name.
But how can I invoke the property setter by the index? Is it possible at all?
解决方案
从QMetaObject
你那里得到QMetaProperty
使用QMetaObject::property(QMetaObject::indexOfProperty(qPrintable(propName)))
,然后你可以打电话QMetaPropety::write(subscriber, value)
(或writeOnGadget()
)。(显然您会存储索引而不是名称,该代码只是示例。)
和/或为了稍微提高效率,您可以使用它QByteArray
来存储属性名称,因为这是与char *
.
推荐阅读
- python - pandas - 在第二个参考数据框中查找最近(但以前)的日期
- php - 必须是数组类型,返回 null Laravel
- java - 如何将bottomnavigationview片段切换到另一个不是菜单项的片段
- visual-studio - 天蓝色函数文件 function.json 在不同的位置
- flutter - 如何在一段时间后取消安排以前安排的本地通知?
- c++ - 如何调用ntoskrnl.exe srand 函数?
- java - 测试@ControllerAdvice NO REST
- ios - 无法分配“字符串??”类型的值 键入“字符串?” 错误
- scala - 使用 fs2 Stream Scala 按顺序处理向队列发送元素
- delphi - 如何检测 Delphi FireMonkey FMX 中的屏幕分辨率变化