我想出了這個解決方案登記圖像提供商功能。它有效,但任何建議或改進都非常受歡迎。
ImageProvider.h
class ImageProvider : public QQuickImageProvider
{
public:
explicit ImageProvider(myModel *myModel);
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
signals:
public slots:
private:
myModel *m_myModel;
};
ImageProvider.cpp
ImageProvider::ImageProvider(myModel *myModel) :
QQuickImageProvider(QQuickImageProvider::Pixmap),
m_myModel(myModel)
{
}
QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
QModelIndex index;
bool foundId = false;
for(int row = 0; row < m_myModel->rowCount(); row++)
{
index = m_myModel->index(row, 0);
QString name = QVariant(m_myModel->data(index, Qt::DisplayRole)).toString();
if(name == id)
{
foundId = true;
break;
}
}
if(!foundId)
return QPixmap();
QIcon icon = m_myModel->data(index, Qt::DecorationRole).value<QIcon>();
QPixmap pixmap = icon.pixmap(128,128);
return pixmap;
}
寄存器imageProvider ...
ImageProvider *imageProvider = new ImageProvider(myModel);
view->engine()->addImageProvider(QLatin1String("provider"), imageProvider);
main.qml
Component {
id: myDelegate
//...
Column {
anchors.verticalCenter: parent.verticalCenter
spacing: 5
Image {
source: "image://provider/" + model.DisplayRole
}
Text {
text: model.DisplayRole
//...
}
來源
2014-02-21 10:15:25
avb
你可否透露更多關於如何返回的像素數據(這顯然必須來自我的模型)? – avb