我有一个模特和一个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;
}
Model.h
class Model : public QAbstractListModel {
Q_OBJECT
public:
enum MyRoles {
PathRole = Qt::UserRole + 1
};
Q_ENUM(MyRoles)
explicit Model(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QHash<int, QByteArray> roleNames() const;
void addIcon(const QString &iconName, const QString &iconPath);
private:
QList<QString> paths;
QMap<QString,int> indexMap;
};
包装器:
Q_PROPERTY(MyModel * modelProperty READ getModel NOTIFY modelChanged)
Qml:
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}
}
}
当我第一次设置图标时,它没有任何错误信息。在第一次分配后,出现错误“无法将[undefined]分配给QUrl”。
我在模型中使用addIcon方法添加了新的图标路径。
您知道为什么会发生这种情况吗?
谢谢。
答案 0 :(得分:1)
警告表示您从data
返回了无效值。
#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;
};
测试QML:
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")
}
}
}