QList 子级 - 从 QObject 派生的结构或自定义类?
我目前正在Symbian 平台上开发Qt 应用程序。应用程序有一个 sqlite 数据库,初始数据是从 txt 文件填充的。
我正在实现 json 格式数据的在线更新。因此,我想在我的数据库更新类中创建通用函数,该函数采用类/结构的 QList 并从中更新数据库。 QList
将填充来自 txt 或 json 的对象。
我已经进行了解析,只是考虑在性能方面什么会更好:
- 创建 C++ 结构并传递它们(因为对象只保存简单数据)包装在
QList
- 创建从
QObject
并将它们作为QList
中的指针传递,然后使用qDeleteAll
删除所有内容 - 任何其他方式...
I'm currently working on Qt application on Symbian platform. Application has an sqlite database and initial data is populated from txt files.
I'm implementing online update from data that comes in json format. So i would like to create generic functions in my db update class that takes QList
of classes/structs and updated db from them. QList
would be populated with objects either from txt, or json.
I already have the parsing in place, just considering what would be better in terms for performance:
- Creating c++ structs and passing them (as objects hold only simple data) wrapped in
QList
- Creating custom classes derived from
QObject
and passing them as pointers inQList
and then deleting everything withqDeleteAll
- Any other way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您的类是否携带行为或仅携带状态。
它们具有行为。
那么,多态类就可以了,是的。是否需要继承
QObject
是另一个问题。仅当您需要 QObject 的服务(内省、信号/槽、事件处理)时才从 QObject 继承。否则,不要。至于
qDeleteAll()
:我不会去那里。使用智能指针代替裸指针,例如QSharedPointer
。他们跟踪对其有效负载的引用数量,并在引用计数降至零时将其删除。在这种情况下,不要使用
QList
,而是使用更高效的容器,例如QVector
。它们只携带状态。
那么,一个“哑”
struct
就足够了。在这种情况下,不要使用QList
作为容器,而是使用更高效的东西,例如QVector
(不要忘记充分利用reserve( )
方法)。一般来说,对于
T
类型(其中sizeof(T)>sizeof(void*)
)和非内置类型,尽量避免使用QList
/non-Qt-types,因为QList
性能会因此降低。That depends on whether your classes are carrying behaviour or only state.
They carry behaviour.
Then, a polymorphic class is in order, yes. Whether it needs to inherit from
QObject
is another question. Inherit fromQObject
only if you need its services (introspection, signals/slots, event handling). Otherwise, don't.As for
qDeleteAll()
: I wouldn't go there. Instead of naked pointers, use smart pointers, e.g.QSharedPointer
. They keep track of the number of references to their payload, deleting it when the refcount falls to zero.In this case, don't use
QList
, but a more efficient container such asQVector
.They carry only state.
Then, a "dumb"
struct
may suffice. In this case, don't useQList
as a container, but something more efficient, likeQVector
(don't forget to make good use of thereserve()
method).In general, try to avoid
QList<T>
for typesT
wheresizeof(T)>sizeof(void*)
and non-buildin/non-Qt-types, becauseQList
performance is degraded for these.