问题描述
我想让一个类暴露一个(或几个)一个QObject派生类(有自己的qt属性)qt属性我可以在qml中使用。
I'm trying to make a class exposing a collection(or several) of a QObject derived class(with its own qt properties) qt properties I can use in qml.
根据 qt在复制构造函数中效果不佳。
According to http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#no-copy-constructor-or-assignment-operator qt doesn't play well with copy constructors.
所以我使用 QList< QObject派生类>
(我的第一个想法)我不能通过引用至少我认为编译器错误意味着什么)(需要副本),我很难知道如何添加项目到列表中。
So I used QList<QObject derived class>
(my first idea) I can't pass the list by reference(or at least I think thats what the compiler errors implies)(needs copies) and am having a hard time figuring out howto add items to the list.
我应该使用 QList< QObject派生类*>
或 QList< SomeQTSmartPointer< QObject派生类>>
>
Should I use QList<QObject derived class *>
or QList<SomeQTSmartPointer<QObject derived class>>
or something else?
推荐答案
$ b b
两者都很好,但是最好通过Qt父/子机制坚持前者。这将是最通常的Qt管理方式:
Both are fine, but it is better to stick to the former through the Qt parent/child mechanism in general. This would be the most Qt'ish way of managing it in general:
QList<MyClass*> list;
list.append(new MyClass(parent);
您也可以使用Qt智能指针例如QPointer,QScopedPointer或QSharedPointer如下:
You could also use Qt smart pointers like QPointer, QScopedPointer or QSharedPointer as follows:
QList<QShardPointer<MyClass> > list;
// Being explicit to be more comprehensive, but it is not necessary
list.append(QSharedPointer<MyClass>(new MyClass());
这将使用预C ++ 11和post。
This will work with both pre C++11 and post.
这篇关于一个对象有一个QObject派生类的集合的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!