PDA

View Full Version : How assign dynamically QPixmap to QML delegate when using QAbstractItemModel in 5.7 ?



matsukan
20th July 2016, 18:23
I do have


TreeView {
id: view
enabled: !settingsData.allDisabled
TableViewColumn {
title: "Icon"
role: "image"
width: 32
delegate: Image {
height: 20
source: "image://icons/" + id
// source: image
}
}
TableViewColumn {
title: "Name"
role: "display"
resizable: true
visible: true
}
TableViewColumn {

And


QHash<int, QByteArray> PasswordListModel::roleNames() const {

QHash<int, QByteArray> result = QAbstractItemModel::roleNames();
result.insert(ImageRole, QByteArrayLiteral("image"));
result.insert(LastAccessedRole, QByteArrayLiteral("lastAccessed"));
result.insert(LastModifiedRole, QByteArrayLiteral("lastModified"));
result.insert(ContainerIdRole, QByteArrayLiteral("containerId"));
return result;
}

and



QVariant PasswordListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();

.
.
.


case ImageRole: {
qDebug () << "ImageRole";
// return QVariant(QString("image://icons/password_icon"));
// return QVariant(QString("password_icon"));
return QString ("password_icon");
}


How to ImagaProvider will get notification to return correct bitmap for QML side. Based on my debugging, MyClass::requestPixmap(const QString &id, QSize */*size*/, const QSize &requestedSize) will not get request from system.

anda_skoa
20th July 2016, 21:30
Maybe you can start by describing what you have.

I assume you images are not on disk or anywhere else that is referencable by URL, right?

How does your application generate them and where/how to you store them in memory?

Cheers,
_

matsukan
21st July 2016, 06:58
It's in the disk, QQuickImageProvider derived class will provided them . Model should show correct icon based on type of item which be different in child list. Icons are included in the project.

anda_skoa
21st July 2016, 09:01
Ok, but if they are on disk, why do you need a custom image provider?

Just let the model return the file URI. see QUrl::fromLocalFile().

Cheers,
_

matsukan
25th July 2016, 17:47
yes, ok. How to assign this URI to delegate in use ?

anda_skoa
26th July 2016, 10:46
Like any other model data



source: styleData.value


Cheers,
_

matsukan
26th July 2016, 21:27
It helped, thanks.