自定义类的QMap无法复制

时间:2015-04-20 12:06:41

标签: c++ qt qmap

当我调用下面的函数时,我得到qmap为空。我读过如果我的自定义类不是从QObject派生出来的,并且有复制构造函数和赋值运算符,它应该可以工作。

QMap<qint32, QMap<qint32, PayInOut> > mPayInOuts;


QMap<qint32, PayInOut> DataManager::getPayInModes()
{
    qDebug() << Q_FUNC_INFO << "Invoked";
    QMap<qint32, PayInOut> payInModes;

    if (mPayInOuts.contains(0))
    {
        qDebug() << Q_FUNC_INFO << "exists";
        payInModes = mPayInOuts.value(0);
    }

    qDebug() << Q_FUNC_INFO << "Exits" << payInModes.count();
    return payInModes;
}

我有一个自定义类,它有复制构造函数和赋值运算符。

class PayInOut
{
public:
    PayInOut():code(0) {}
    PayInOut(const PayInOut &inOut):code(inOut.code), desc(inOut.desc),
                                    imagePixmap(inOut.imagePixmap) { }
    qint32 code;
    QString desc;
    QPixmap imagePixmap;

    PayInOut& operator= (const PayInOut &other)
    {
        if (this != &other)
        {
            this->code = other.code;
            this->desc = other.desc;
            this->imagePixmap = other.imagePixmap;
        }

        return *this;
    }
};

0 个答案:

没有答案