gpt4 book ai didi

qt - QScopedPointers 的 QList

转载 作者:行者123 更新时间:2023-12-02 20:59:23 33 4
gpt4 key购买 nike

我正在尝试将 QScopedPointers 存储在 QList 中。

我发现了这条评论

One can also use QList >. – Kuba Ober Jan 14 '14 at 18:17

(对此答案的第一条评论:https://stackoverflow.com/a/21120575/3095014)

还有这篇文章https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers这意味着这应该有效。但是如果我尝试编译第二个链接的代码,我会收到以下错误:

E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class 'QScopedPointer<Label,QScopedPointerDeleter<T>>'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qscopedpointer.h(170) : see declaration of 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(403) : while compiling class template member function 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(553) : see reference to function template instantiation 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)' being compiled
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(794) : while compiling class template member function 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::~QList(void)'
with
[
T=Label
]
..\tableview_row_dnd\main.cpp(13) : see reference to function template instantiation 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::~QList(void)' being compiled
with
[
T=Label
]
..\tableview_row_dnd\main.cpp(20) : see reference to class template instantiation 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>' being compiled
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(405) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class 'QScopedPointer<Label,QScopedPointerDeleter<T>>'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qscopedpointer.h(170) : see declaration of 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer'
with
[
T=Label
]

为什么这对我不起作用?

最佳答案

存储在 Qt 容器中的值应该是可分配的数据类型。这意味着它们应该有一个默认构造函数、一个复制构造函数和一个赋值运算符。

QScopedPointer 已禁用其复制构造函数和赋值运算符。您不能将两个指针互相分配,但可以使用 QScopedPointer::reset、QScopedPointer::swap 或 显式转移底层原始指针的所有权>QScopedPointer::take.

At some point QScopedPointer 中添加了移动构造函数和移动赋值运算符。新的移动语义使这成为可能:

QList<QScopedPointer<Label>> mLabels;
mLabels.append(QScopedPointer<Label>(new Label));

这里将临时值添加到列表中,并使用移动构造函数创建新的列表项。

Later他们恢复了该更改:

Adding a move contructor to QScopedPointer makes no sense, because moving means 'escaping the scope', which breaks the fundamental point of QScopedPointer.

如果你确实想要一个智能指针列表,你可以使用可赋值的QSharedPointer或支持移动语义的std::unique_ptr

如果您谈论跟踪 QObjects 子类(尤其是小部件)的生命周期,我建议使用 Qt 子父机制而不是智能指针。

关于qt - QScopedPointers 的 QList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761327/

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