gpt4 book ai didi

QT:问题,如何将我自己的 QObject 派生自定义类作为 “QVariant” 返回?

转载 作者:行者123 更新时间:2023-12-04 13:02:54 24 4
gpt4 key购买 nike

实现派生的“QAbstractListModel::data”方法。

Q_DECLARE_METATYPE(myType);甚至不编译......返回我的自定义对象会导致编译错误。

如何做到这一点?

最佳答案

QVariant::fromValue<QObject *>(object);

用您自己的类型替换 QObject,但在其上使用 Q_DECLARE_METATYPE。请记住您要声明的内容:MyTypeMyType *。由于您正在谈论从 QAbstractItemModel::data 传递一个对象,那么我想您想要提供一个指向该对象的指针,这是正确的吗?如果是这样,那么声明如下:

typedef MyType * MyTypeStar
Q_DECLARE_METATYPE(MyTypeStar);

这将使 MyType * 为元类型系统所知。

如果你想传递对象本身,那么声明你尝试过的方式,但要确保你正确定义了你的类型:

Q_DECLARE_METATYPE: This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.

关于QT:问题,如何将我自己的 QObject 派生自定义类作为 “QVariant” 返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5355649/

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