【问题标题】:QList of QScopedPointersQScopedPointers 的 QList
【发布时间】:2016-01-13 08:06:41
【问题描述】:

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

我找到了这条评论

也可以使用 QList >。 – Kuba Ober 2014 年 1 月 14 日 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 containers qt5 smart-pointers qscopedpointer


【解决方案1】:

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

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

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

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

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

Later 他们恢复了该更改:

向 QScopedPointer 添加移动构造函数没有意义,因为 移动意味着“逃避范围”,它打破了基本点 的 QScopedPointer。

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

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

【讨论】:

  • 请注意,与 STL 容器相比,对可分配数据类型的要求极不理想。在某些情况下(例如:默认可构造性),需求源于糟糕的实现。解决方法:如果您真的想要unique_ptr 的向量,请使用 STL 容器而不是 Qt 容器。
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2013-07-04
  • 2011-09-29
  • 2010-10-21
相关资源
最近更新 更多