5

I have an image provider derived from QQuickImageProvider with an implementation of requestPixmap.

This image provider works fine with an Image component.

Now I want to provide the imageSource for a Button in the same way. But no image shows up. What can be the problem?

QML code

Image {
    anchors.fill: parent
    anchors.margins: 10
    source: "image://provider/" + model.DisplayRole
    fillMode: Image.PreserveAspectFit
}

Button {
    Layout.fillWidth: true
    Layout.preferredHeight: width
    iconSource: "image://provider/" + model.DisplayRole
    onClicked: appCore.switch(model.DisplayRole)
}

C++ code

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;;
}
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
avb
  • 1,701
  • 5
  • 22
  • 37

0 Answers0