Can't store QIcon in private class member

266 views Asked by At

I have model inherited from QAbstractItemModel:

class Plugin: public QObject
{
    Q_OBJECT

public:
    explicit Plugin(QObject *parent=0);
    ~Plugin();

    const QIcon icon();
    void setIcon(const QString &ico);
private:
    QIcon mIcon;
};

class PluginsModel: public QAbstractItemModel
{
    Q_OBJECT

public:
    explicit PluginsModel(QObject *parent=0);
    ~PluginsModel();
protected:
    QList<Plugin*> mList;
};

I want to create QIcon instance only once and in data just do:

QVariant PluginsModel::data(const QModelIndex &index, int role) const
{
    if (mList.isEmpty())
        return QVariant();

    Plugin *it = static_cast<Plugin*>(index.internalPointer());
    switch (role ) {
    case Qt::DecorationRole:
        switch (index.column()) {
        case 0:
            return it->icon();
            break;
        default:
            break;
        }
    }

    return QVariant();
}

So setIcon function looks like:

const QIcon Plugin::icon()
{
    return mIcon;
}

void Plugin::setIcon(const QString &ico)
{
    mIcon = QIcon(ico);
}

And usage:

Plugin *p = new Plugin(this);
p->setIcon(":/images/16/amarok.png");
mPlugins->add(p);

The problem is that it doesn't work (icon is not drawed). But if I change setIcon to:

void Plugin::setIcon(QIcon &ico)
{
    mIcon = ico;
}

... and usage to:

Plugin *p = new Plugin(this);
QIcon i(":/images/16/amarok.png");
p->setIcon(i);
mPlugins->add(p);

... then it is working fine. Anyone can tell me what is a difference between passing QIcon reference from outside and creating reference inside class from image path argument?

0

There are 0 answers