Qt 无法将[未定义]分配给QUrl

Qt 无法将[未定义]分配给QUrl,qt,qml,qtquick2,qabstractlistmodel,Qt,Qml,Qtquick2,Qabstractlistmodel,我有一个模型和一个qml Model.cpp: Model::Model(QObject *parent): QAbstractListModel(parent) { } void Model::addIcon(const QString &iconName, const QString &iconPath) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); if(!indexMap.contai

我有一个模型和一个qml

Model.cpp:

Model::Model(QObject *parent): QAbstractListModel(parent)
{
}

void Model::addIcon(const QString &iconName, const QString &iconPath)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    if(!indexMap.contains(iconName)) {
        paths.append(iconPath);
        indexMap.insert(iconName,paths.indexOf(iconPath));
    }
    else {

        //Update the icon for the designated icon
        paths[indexMap.value(iconName)] = iconPath;
        QModelIndex index = createIndex(indexMap.value(iconName),1);
        emit dataChanged(index,index);
    }

    endInsertRows();
}

QVariant Model::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= indexMap.count())
        return QVariant();

    return paths.at(index.row());
}

int Model::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return paths.count();
}

QHash<int, QByteArray> Model::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[PathRole] = "iconImage";
    return roles;
}
当我第一次设置图标时,它不会给我任何错误消息。在第一次赋值之后,我得到错误“无法将[undefined]赋值给QUrl”

我使用addIcon方法在模型中添加新图标路径

你知道为什么会这样吗


提前感谢。

警告意味着您从
数据中返回了无效值

#include <QAbstractListModel>

#define Invalid -1

class Model : public QAbstractListModel
{
    Q_OBJECT
public:
    struct Icon {
        QString name;
        QString path;
    };
    Q_INVOKABLE void add(QString name, QString path) {
        add({name, path});
    }
    void add(const Icon& icon) {
        const int& i = find(icon.name);
        if(i != Invalid) {
            mIconPaths[i].path = icon.path;
            emit dataChanged(index(i), index(i)); // Do not add beginInsertRows
        } else {
            beginInsertRows(QModelIndex(), rowCount(), rowCount());
            mIconPaths.push_back(icon);
            endInsertRows();
        }
    }

    int find(const QString& name) {
        for(int i = 0; i < mIconPaths.size(); i++)
            if(mIconPaths[i].name == name)
                return i;
        return Invalid;
    }

    QVariant data(const QModelIndex &index, int role) const
    {
        Q_UNUSED(role)
        if (index.row() < 0 || index.row() >= rowCount())
            return QVariant();
        return mIconPaths.at(index.row()).path;
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const
    {
        Q_UNUSED(parent)
        return mIconPaths.count();
    }

    QHash<int, QByteArray> roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[0] = "icon";
        return roles;
    }

private:
    QList<Icon> mIconPaths;
};

@我添加了头文件,但忘了提到,图标路径已正确更新。我的意思是我刚刚得到一个警告。@huckfin什么值​​iconPath接受吗?@eyllanesc它只接受图标的路径作为QString。例如:qrc:/warningIcon@huckfinshow MyService.hplease@eyllanesc它只是通过传递图标的路径字符串来调用addIcon()函数。不过,我有一个包装,这可能是相关的,我刚刚添加到后。非常感谢。它可以工作,但我想了解我的代码出了什么问题。是Q_可调用还是dataChanged信号使其工作?@huckfin beginInsertRows&dataChanged是不同的操作。在修改数据时,您同时执行了这两项操作。beginInsertRows告诉ListView已插入,dataChanged告诉ListView已修改。好的,然后我所做的是尝试添加和修改。再次感谢您的澄清。@huckfin和另一个问题,QAbstractListModel是一个列表,那么为什么不使用列表来存储数据而不是地图呢。这让我一开始对这个问题感到困惑。Q_只能用于演示。
import QtQuick 2.5

Rectangle{
width:100
height:30

    ListView{
        id:icons

        property alias myModel: myService.modelProperty

        anchors.fill: parent
        anchors.topMargin: 10
        anchors.rightMargin: 20

        model: myModel

        delegate: Image{source: iconImage}
    }
}
#include <QAbstractListModel>

#define Invalid -1

class Model : public QAbstractListModel
{
    Q_OBJECT
public:
    struct Icon {
        QString name;
        QString path;
    };
    Q_INVOKABLE void add(QString name, QString path) {
        add({name, path});
    }
    void add(const Icon& icon) {
        const int& i = find(icon.name);
        if(i != Invalid) {
            mIconPaths[i].path = icon.path;
            emit dataChanged(index(i), index(i)); // Do not add beginInsertRows
        } else {
            beginInsertRows(QModelIndex(), rowCount(), rowCount());
            mIconPaths.push_back(icon);
            endInsertRows();
        }
    }

    int find(const QString& name) {
        for(int i = 0; i < mIconPaths.size(); i++)
            if(mIconPaths[i].name == name)
                return i;
        return Invalid;
    }

    QVariant data(const QModelIndex &index, int role) const
    {
        Q_UNUSED(role)
        if (index.row() < 0 || index.row() >= rowCount())
            return QVariant();
        return mIconPaths.at(index.row()).path;
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const
    {
        Q_UNUSED(parent)
        return mIconPaths.count();
    }

    QHash<int, QByteArray> roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[0] = "icon";
        return roles;
    }

private:
    QList<Icon> mIconPaths;
};
Window
{
    visible: true
    height: 640
    width: 480

    ListView {
        anchors.fill: parent
        model: myModel
        delegate: Text{
            text: icon
        }
        Component.onCompleted: {
            myModel.add("test", "test")
            myModel.add("test", "modified")
            myModel.add("test2", "added")
        }
    }
}