有没有办法在QMap值中设置一个类?

时间:2016-03-09 07:44:38

标签: c++ qt singleton qmap

有没有办法添加&在QMap值中使用一个类?

我想在Qt中使用QMap<QString, Aclass> map;。当我想在函数中设置它的值时,会出现一些错误:

C:\Qt\Qt5.5.0\5.5\mingw492_32\include\QtCore\qglobal.h:1043: error: 'QWidget& QWidget::operator=(const QWidget&)' is private
     Class &operator=(const Class &) Q_DECL_EQ_DELETE;
            ^

ps:我的容器类继承自QWidget&amp;是单身。

#include "Aclass.h"
#include <QWidget>

class AmainClass : public QWidget
{
    Q_OBJECT

public:
    static AmainClass &getInstance();
    void setApp(QString name, Aclass app);

private:
    AmainClass(QWidget *parent = 0);
    QMap<QString, Aclass> map;
};

和.cpp:

void AmainClass::setApp(QString name, Aclass app)
{
    map.insert(name, app);
}

编辑:Aclass是另一个继承自QWidget

的类

3 个答案:

答案 0 :(得分:2)

根据Qt文档,QObject和从它派生的类不支持被复制:Qt的元对象机制和类似的基础结构依赖于指向QObject的指针仍然有效,如果复制或移动它们将不会成立。因此,您无法按值在容器中存储QObject。改为使用智能指针:

QMap<QString, std::unique_ptr<Aclass>> map;

同样适用于setApp,当然:

void AmainClass::setApp(QString name, std::unique_ptr<Aclass> app)
{
    map.insert(name, std::move(app));
}

使用std::unique_ptr假设有三件事:

  1. 您使用的是支持C ++ 11部分的编译器。
  2. 您的Qt版本最近已足以支持移动语义。
  3. 您希望map成为Aclass个对象的唯一所有者。
  4. 由于这是2016年,1&amp; 2现在应该是真的。

    对于3,您通常需要在选择实施方式(编码决策)之前理清所有权(设计决策)。根据您的原始示例,我假设您希望map拥有Aclass个对象,因此我使用了std::unique_ptr

    如果您想要使用其他所有权方案(例如使用Qt的父子关系),您可以使用不同的存储空间。对于父子事物,原始指针(或者QPointer)是合适的。

答案 1 :(得分:1)

无法复制从QObject派生的所有Qt对象,因为复制构造函数是私有的。这是错误消息。您可以在地图中使用引用或指向对象的指针

QMap<QString, Aclass*> map;

void setApp(QString name, Aclass& app){
map.insert(name, app);
}

然后设置函数应该是

Sub FilterTo1Criteria()
    Dim a As Long, r As Long, c As Long, vals As Variant
    Dim xlSheet As Worksheet
    'Set xlbook = GetObject("C:\07509\04-LB-06 MX-sv.xlsx")
    Set xlSheet = Worksheets("04-LB-06 MX")
    With xlSheet
        If .AutoFilterMode Then .AutoFilterMode = False

        'With .Range("blockn")
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=1, Criteria1:="SV-PCS7"
            'step off the header row
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                'check if there are visible cells
                If CBool(Application.Subtotal(103, .Cells)) Then
                    'dimension the array (backwards)
                    ReDim vals(1 To .Columns.Count, 1 To 1)
                    'loop through the areas
                    For a = 1 To .SpecialCells(xlCellTypeVisible).Areas.Count
                        With .SpecialCells(xlCellTypeVisible).Areas(a)
                            'loop through the rows in each area
                            For r = 1 To .Rows.Count
                                'put the call values in backwards because we cannot redim the 'row'
                                For c = LBound(vals, 1) To UBound(vals, 1)
                                    vals(c, UBound(vals, 2)) = .Cells(r, c).Value
                                Next c
                                'make room for the next
                                ReDim Preserve vals(1 To UBound(vals, 1), 1 To UBound(vals, 2) + 1)
                            Next r
                        End With
                    Next a
                End If
            End With
        End With

        If .AutoFilterMode Then .AutoFilterMode = False
    End With


    'trim off the last empty 'row'
    ReDim Preserve vals(1 To UBound(vals, 1), 1 To UBound(vals, 2) - 1)
    'reorient the array
    vals = Application.Transpose(vals)
    'show the extents
    Debug.Print LBound(vals, 1) & ":" & UBound(vals, 1)
    Debug.Print LBound(vals, 2) & ":" & UBound(vals, 2)

    'show the values
    For r = LBound(vals, 1) To UBound(vals, 1)
        For c = LBound(vals, 2) To UBound(vals, 2)
            Debug.Print vals(r, c)
        Next c
    Next r

End Sub

当你使用指针时,只需替换&amp;与*

答案 2 :(得分:0)

您可以使用compileSdkVersion 23 targetSdkVersion 23 作为值的类型 无论如何,请注意您必须保证引用对象的生命周期。 有关详细信息,请参阅documentation

否则,您可以使用std::reference_wrapper<Aclass>std::unique_ptr<Aclass>等智能指针,以便为您自动管理生命周期。
有关std::shared_ptr<Aclass>的更多详细信息,请参阅hereshared_ptr {/ 3}}。

否则,更简单的是,您可以为unique_ptr定义错过的运算符operator=。几乎取决于你的实际问题,我甚至不能说它是否是一个可行的解决方案。

相关问题