简体   繁体   中英

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. Sometimes write stl seems me like resolving rebus in a very verbose way.

So I'm just wondering if there are some side effects in using instances of Qt Containers inside some non QObjects derivate

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. 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; 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.

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; also, from C++11 STL containers support move semantics, while currently Qt containers just copy the objects around.

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).

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 containers are completely unrelated to QObject functionality. Qt itself is using lots of containers of non-QObject-derived classes - QStringList is a common example. Qt itself is also using containers in methods in non-QObject-derived classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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