gpt4 book ai didi

c++ - 从 QObject 中删除动态属性

转载 作者:行者123 更新时间:2023-11-30 02:22:58 26 4
gpt4 key购买 nike

有没有办法删除分配给 QObject 的所有动态属性?

最佳答案

试试这个:

for (auto const &name : object->dynamicPropertyNames())
{
object->setProperty(name.constData(), {});
}
Q_ASSERT(object->dynamicPropertyNames().empty());

这会遍历所有动态属性并设置空的 QVariant 值。如果断言失败,那么这是一个 Qt 错误,因为文档说:

http://doc.qt.io/qt-5/qobject.html#dynamic-properties

From Qt 4.2, dynamic properties can be added to and removed from QObject instances at run-time.

Dynamic properties ... can be removed by setting the property value to an invalid QVariant.

关于c++ - 从 QObject 中删除动态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46691526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com