简体   繁体   English

非QObject内的Qt容器

[英]Qt containers inside non QObject

After working some times with Qt containers and switch back to c++ stl I have found that I'm much more comfortable with Qt containers. 使用Qt容器一段时间后,切换回c ++ stl,我发现我对Qt容器更加满意。 Sometimes write stl seems me like resolving rebus in a very verbose way. 有时写stl似乎让我喜欢以一种非常冗长的方式解决重用问题。

So I'm just wondering if there are some side effects in using instances of Qt Containers inside some non QObjects derivate 所以我只是想知道在一些非QObject派生类中使用Qt容器实例是否有一些副作用

Well, take a look at the different classes you want to have and check if there are any dependencies. 好吧,看看您想要拥有的不同类,并检查是否存在任何依赖关系。 For example, QList doesn't depend on any specific Qt Objects, is not a Q_OBJECT as far as I'm aware and should be good to go. 例如, QList不依赖于任何特定的Qt对象,据我所知不是Q_OBJECT ,应该很好。 The same probably goes for all the other container classes listed here . 这里列出的所有其他容器类可能也是如此。

What you do have to care about is licensing though. 不过,您要做的只是许可。 As this is code available under very specific rules, you cannot simply take these classes and use them in your commercial applications, for example. 由于这是在非常特定的规则下可用的代码,因此,例如,您不能简单地采用这些类并在商业应用程序中使用它们。 - Edit: Well you can, as long as you dinamically link them, of course. -编辑:当然,只要您可以动态链接它们,就可以。 I didn't mean to say "it's not possible" but to have the emphasis on the "simply". 我并不是说“不可能”,而是要强调“简单”。

Qt containers can be used with "regular" objects without problems; Qt容器可以与“常规”对象一起使用而不会出现问题。 still, keep in mind that they do not exploit some C++ features that STL containers use, so they may place additional requirements on your classes. 仍然要记住,它们没有利用STL容器使用的某些C ++功能,因此它们可能会对您的类提出其他要求。

For example, QVector (as well as other Qt containers) needs a default constructor, and, when adding an element, it does a default construction + assignment, while STL uses placement new to use just the copy constructor; 例如, QVector (以及其他Qt容器)需要一个默认构造函数,并且在添加元素时,它会执行默认构造+赋值,而STL使用new放置仅使用副本构造函数; also, from C++11 STL containers support move semantics, while currently Qt containers just copy the objects around. 同样,从C ++ 11开始,STL容器支持移动语义,而当前的Qt容器只是在周围复制对象。

These are less of a problem for many Qt objects, since "big" Qt objects typically have a "default" invalid state and use implicit sharing semantics (so default construction, copy constructor and assignment are typically cheap), but may be a problem if your objects have costly copy (which may come from embedding STL containers, that have "full copy" semantics). 对于许多Qt对象而言,这些问题不大,因为“大” Qt对象通常具有“默认”无效状态并使用隐式共享语义(因此默认构造,副本构造函数和赋值通常很便宜),但如果您的对象具有昂贵的副本(可能来自嵌入具有“完整副本”语义的STL容器)。

Also, if you plan to distribute your programs in some way, you should consider if it's worth to bring in a quite heav-ish dependency like Qt just for container classes. 另外,如果您打算以某种方式分发程序,则应考虑是否值得为容器类引入像Qt这样的繁重依赖项。

如果您执行正确的包含,则应该可以使用它们!

Qt containers are completely unrelated to QObject functionality. Qt容器与QObject功能完全无关。 Qt itself is using lots of containers of non-QObject-derived classes - QStringList is a common example. Qt本身正在使用许多非QObject派生类的容器QStringList是一个常见示例。 Qt itself is also using containers in methods in non-QObject-derived classes. Qt本身也在非QObject派生类的方法中使用容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM